linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ard.biesheuvel@linaro.org>
To: linux-kernel@vger.kernel.org, mpe@ellerman.id.au, jeyu@redhat.com
Cc: rusty@rustcorp.com.au, suzuki.poulose@arm.com,
	will.deacon@arm.com, akpm@linux-foundation.org,
	benh@kernel.crashing.org, paulus@samba.org, arnd@arndb.de,
	torvalds@linux-foundation.org, viro@zeniv.linux.org.uk,
	linuxppc-dev@lists.ozlabs.org,
	Ard Biesheuvel <ard.biesheuvel@linaro.org>
Subject: [PATCH v4 2/3] powerpc/reloc64: add support for 32-bit CRC pseudo-symbols
Date: Tue, 17 Jan 2017 19:26:12 +0000	[thread overview]
Message-ID: <1484681173-11644-3-git-send-email-ard.biesheuvel@linaro.org> (raw)
In-Reply-To: <1484681173-11644-1-git-send-email-ard.biesheuvel@linaro.org>

In preparation of modifying the core modversions code to emit the CRCs
as 32-bit quantities, ensure that 64-bit PowerPC will be able to deal
with this when CONFIG_RELOCATABLE=y, in which case the CRCs will be
emitted into the final ELF binary as R_PPC64_ADDR32 relocations.

Since 32-bit relocations cannot be used to relocate memory addresses on
64-bit architectures, and since the CRC pseudo-symbol references are
emitted as anonymous relocations (i.e., against the NULL symbol in the
.dynsym section) with the final value recorded in the addend (*), we
can disregard any relocations where the symbol index != 0.

* Note that unsatisfied CRC pseudo-symbol references are emitted as
  R_PPC64_ADDR32 relocations against named symbols that are typed as
  weak undefined in the .dynsym symbol table. These can simply be
  ignored (as before), considering that zero CRCs are interpreted as
  missing, and the module code deals with that accordingly.

As it turns out, binutils for powerpc does not account for any relocations
beyond R_PPC64_RELATIVE ones in the RELACOUNT field of the .dynamic section,
which is unfortunate, since we need to do extra work to figure out the size
of the relocation array. So with a little help from the linker scripts,
grab an end pointer rather than a count, and iterate over the entire section.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 arch/powerpc/kernel/reloc_64.S    | 60 ++++++++++++--------
 arch/powerpc/kernel/vmlinux.lds.S |  1 +
 arch/powerpc/relocs_check.sh      |  5 +-
 3 files changed, 40 insertions(+), 26 deletions(-)

diff --git a/arch/powerpc/kernel/reloc_64.S b/arch/powerpc/kernel/reloc_64.S
index d88736fbece6..e50f5d778ea2 100644
--- a/arch/powerpc/kernel/reloc_64.S
+++ b/arch/powerpc/kernel/reloc_64.S
@@ -12,8 +12,8 @@
 #include <asm/ppc_asm.h>
 
 RELA = 7
-RELACOUNT = 0x6ffffff9
 R_PPC64_RELATIVE = 22
+R_PPC64_ADDR32 = 1
 
 /*
  * r3 = desired final address of kernel
@@ -29,29 +29,27 @@ _GLOBAL(relocate)
 	add	r9,r9,r12	/* r9 has runtime addr of .rela.dyn section */
 	ld	r10,(p_st - 0b)(r12)
 	add	r10,r10,r12	/* r10 has runtime addr of _stext */
+	ld	r8,(p_rela_end - 0b)(r12)
+	add	r8,r8,r12	/* r8 has addr of end of .rela.dyn section */
 
 	/*
-	 * Scan the dynamic section for the RELA and RELACOUNT entries.
+	 * Scan the dynamic section for the RELA entry.
+	 * NOTE: the RELACOUNT entry only covers R_PPC64_RELATIVE relocations,
+	 *       so we cannot use it here.
 	 */
 	li	r7,0
-	li	r8,0
 1:	ld	r6,0(r11)	/* get tag */
 	cmpdi	r6,0
-	beq	4f		/* end of list */
+	beq	3f		/* end of list */
 	cmpdi	r6,RELA
-	bne	2f
-	ld	r7,8(r11)	/* get RELA pointer in r7 */
-	b	3f
-2:	addis	r6,r6,(-RELACOUNT)@ha
-	cmpdi	r6,RELACOUNT@l
-	bne	3f
-	ld	r8,8(r11)	/* get RELACOUNT value in r8 */
-3:	addi	r11,r11,16
+	beq	2f
+	addi	r11,r11,16
 	b	1b
-4:	cmpdi	r7,0		/* check we have both RELA and RELACOUNT */
-	cmpdi	cr1,r8,0
-	beq	6f
-	beq	cr1,6f
+2:	ld	r7,8(r11)	/* get RELA pointer in r7 */
+3:	cmpdi	r7,0		/* check we have both RELA and a non-empty */
+	cmpd	cr1,r8,r9	/* .rela.dyn section			   */
+	beq	7f
+	beq	cr1,7f
 
 	/*
 	 * Work out linktime address of _stext and hence the
@@ -63,26 +61,40 @@ _GLOBAL(relocate)
 	subf	r7,r7,r9	/* cur_offset */
 	subf	r10,r7,r10
 	subf	r3,r10,r3	/* final_offset */
+	b	4f
 
 	/*
 	 * Run through the list of relocations and process the
-	 * R_PPC64_RELATIVE ones.
+	 * R_PPC64_RELATIVE and R_PPC64_ADDR32 ones.
 	 */
-	mtctr	r8
-5:	ld	r0,8(9)		/* ELF64_R_TYPE(reloc->r_info) */
+3:	addi	r9,r9,24
+4:	cmpd	r9,r8
+	beq	7f
+5:	ld	r0,8(9)		/* reloc->r_info (type *and* symbol index) */
+	ld	r6,0(r9)	/* reloc->r_offset */
 	cmpdi	r0,R_PPC64_RELATIVE
 	bne	6f
-	ld	r6,0(r9)	/* reloc->r_offset */
 	ld	r0,16(r9)	/* reloc->r_addend */
 	add	r0,r0,r3
 	stdx	r0,r7,r6
-	addi	r9,r9,24
-	bdnz	5b
+	b	3b
+
+	/*
+	 * CRCs of exported symbols are emitted as 32-bit relocations against
+	 * the NULL .dynsym entry, with the CRC value recorded in the addend.
+	 */
+6:	cmpdi	r0,R_PPC64_ADDR32
+	bne	3b
+	ld	r0,16(r9)	/* reloc->r_addend */
+	stwx	r0,r7,r6
+	b	3b
+
+7:	blr
 
-6:	blr
 
 .balign 8
 p_dyn:	.llong	__dynamic_start - 0b
 p_rela:	.llong	__rela_dyn_start - 0b
+p_rela_end:
+	.llong	__rela_dyn_end - 0b
 p_st:	.llong	_stext - 0b
-
diff --git a/arch/powerpc/kernel/vmlinux.lds.S b/arch/powerpc/kernel/vmlinux.lds.S
index 7394b770ae1f..654728fc860d 100644
--- a/arch/powerpc/kernel/vmlinux.lds.S
+++ b/arch/powerpc/kernel/vmlinux.lds.S
@@ -241,6 +241,7 @@ SECTIONS
 	{
 		__rela_dyn_start = .;
 		*(.rela*)
+		__rela_dyn_end = .;
 	}
 #endif
 	/* .exit.data is discarded at runtime, not link time,
diff --git a/arch/powerpc/relocs_check.sh b/arch/powerpc/relocs_check.sh
index ec2d5c835170..f9636b4e4548 100755
--- a/arch/powerpc/relocs_check.sh
+++ b/arch/powerpc/relocs_check.sh
@@ -30,7 +30,7 @@ bad_relocs=$(
 	# On PPC64:
 	#	R_PPC64_RELATIVE, R_PPC64_NONE
 	#	R_PPC64_ADDR64 mach_<name>
-	#	R_PPC64_ADDR64 __crc_<name>
+	#	R_PPC64_ADDR32 __crc_<name>
 	# On PPC:
 	#	R_PPC_RELATIVE, R_PPC_ADDR16_HI,
 	#	R_PPC_ADDR16_HA,R_PPC_ADDR16_LO,
@@ -43,7 +43,8 @@ R_PPC_ADDR16_HA
 R_PPC_RELATIVE
 R_PPC_NONE' |
 	grep -E -v '\<R_PPC64_ADDR64[[:space:]]+mach_' |
-	grep -E -v '\<R_PPC64_ADDR64[[:space:]]+__crc_'
+	grep -E -v '\<R_PPC64_ADDR32[[:space:]]+__crc_' |
+	grep -E -v '\<R_PPC64_ADDR32[[:space:]]+\*ABS\*'
 )
 
 if [ -z "$bad_relocs" ]; then
-- 
2.7.4

  parent reply	other threads:[~2017-01-17 19:26 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-17 19:26 [PATCH v4 0/3] modversions: Fix CRC mangling under CONFIG_RELOCATABLE=y Ard Biesheuvel
2017-01-17 19:26 ` [PATCH v4 1/3] powerpc/reloc32: fix corrupted modversion CRCs Ard Biesheuvel
2017-01-17 19:26 ` Ard Biesheuvel [this message]
2017-01-18 15:30   ` [PATCH v4 2/3] powerpc/reloc64: add support for 32-bit CRC pseudo-symbols Ard Biesheuvel
2017-01-17 19:26 ` [PATCH v4 3/3] modversions: treat symbol CRCs as 32 bit quantities on 64 bit archs Ard Biesheuvel
2017-01-17 23:47   ` Linus Torvalds
2017-01-18 11:37     ` Ard Biesheuvel
2017-01-18 13:52       ` Ard Biesheuvel
2017-01-18 18:27         ` Linus Torvalds
2017-01-18 18:35           ` Linus Torvalds
2017-01-18 22:37             ` Ard Biesheuvel
2017-01-19  0:15               ` Linus Torvalds
2017-01-19  9:22                 ` Ard Biesheuvel
2017-01-19 17:24                   ` Linus Torvalds
2017-01-20 12:21                     ` Ard Biesheuvel
2017-01-19 17:01         ` David Laight

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1484681173-11644-3-git-send-email-ard.biesheuvel@linaro.org \
    --to=ard.biesheuvel@linaro.org \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=benh@kernel.crashing.org \
    --cc=jeyu@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=mpe@ellerman.id.au \
    --cc=paulus@samba.org \
    --cc=rusty@rustcorp.com.au \
    --cc=suzuki.poulose@arm.com \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=will.deacon@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).