From mboxrd@z Thu Jan 1 00:00:00 1970 From: David Gibson Subject: Re: [kvm-unit-tests PATCH v4 13/17] powerpc/ppc64: relocate linker VMAs Date: Wed, 17 Feb 2016 13:14:05 +1100 Message-ID: <20160217021405.GF5239@voom.redhat.com> References: <1455544166-19766-1-git-send-email-drjones@redhat.com> <1455544166-19766-14-git-send-email-drjones@redhat.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="z+pzSjdB7cqptWpS" Cc: kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, thuth@redhat.com, dgibson@redhat.com, agraf@suse.de, lvivier@redhat.com, pbonzini@redhat.com To: Andrew Jones Return-path: Received: from ozlabs.org ([103.22.144.67]:59847 "EHLO ozlabs.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756612AbcBQGuW (ORCPT ); Wed, 17 Feb 2016 01:50:22 -0500 Content-Disposition: inline In-Reply-To: <1455544166-19766-14-git-send-email-drjones@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: --z+pzSjdB7cqptWpS Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Feb 15, 2016 at 02:49:22PM +0100, Andrew Jones wrote: > QEMU loads the unit test, but due to the way it translates the > unit test's linker VMA to the LMA, we can't just link such that > VMA =3D=3D LMA. Thus, we link with VMA =3D=3D 0x0, and then deal with > relocation. >=20 > Signed-off-by: Andrew Jones > Tested-by: Laurent Vivier Reviewed-by: David Gibson > --- > configure | 2 ++ > powerpc/Makefile.common | 11 ++++++++-- > powerpc/Makefile.ppc64 | 1 + > powerpc/cstart64.S | 34 ++++++++++++++++++++++++++---- > powerpc/flat.lds | 13 +++++++++++- > powerpc/reloc64.c | 55 +++++++++++++++++++++++++++++++++++++++++++= ++++++ > 6 files changed, 109 insertions(+), 7 deletions(-) > create mode 100644 powerpc/reloc64.c >=20 > diff --git a/configure b/configure > index b367224093369..b2ad199da7873 100755 > --- a/configure > +++ b/configure > @@ -5,6 +5,7 @@ kerneldir=3D/lib/modules/$(uname -r)/build > cc=3Dgcc > ld=3Dld > objcopy=3Dobjcopy > +objdump=3Dobjdump > ar=3Dar > arch=3D`uname -m | sed -e s/i.86/i386/ | sed -e 's/arm.*/arm/'` > host=3D$arch > @@ -132,6 +133,7 @@ PROCESSOR=3D$processor > CC=3D$cross_prefix$cc > LD=3D$cross_prefix$ld > OBJCOPY=3D$cross_prefix$objcopy > +OBJDUMP=3D$cross_prefix$objdump > AR=3D$cross_prefix$ar > API=3D$api > TEST_DIR=3D$testdir > diff --git a/powerpc/Makefile.common b/powerpc/Makefile.common > index de4d1ef3ac816..9654bce1752eb 100644 > --- a/powerpc/Makefile.common > +++ b/powerpc/Makefile.common > @@ -22,6 +22,7 @@ CFLAGS +=3D -Wextra > CFLAGS +=3D -O2 > CFLAGS +=3D -I lib -I lib/libfdt > CFLAGS +=3D -Wa,-mregnames > +CFLAGS +=3D -fpie > =20 > asm-offsets =3D lib/$(ARCH)/asm-offsets.h > include scripts/asm-offsets.mak > @@ -37,11 +38,17 @@ libgcc :=3D $(shell $(CC) $(machine) --print-libgcc-f= ile-name) > start_addr :=3D $(shell printf "%x\n" $$(( $(phys_base) + $(kernel_offse= t) ))) > =20 > FLATLIBS =3D $(libcflat) $(LIBFDT_archive) $(libgcc) > -%.elf: LDFLAGS =3D $(CFLAGS) -nostdlib > +%.elf: LDFLAGS =3D $(CFLAGS) -nostdlib -pie > %.elf: %.o $(FLATLIBS) powerpc/flat.lds > $(CC) $(LDFLAGS) -o $@ \ > -Wl,-T,powerpc/flat.lds,--build-id=3Dnone,-Ttext=3D$(start_addr) \ > $(filter %.o, $^) $(FLATLIBS) > + @echo -n Checking $@ for unsupported reloc types... > + @if $(OBJDUMP) -R $@ | grep R_ | grep -v R_PPC64_RELATIVE; then \ > + false; \ > + else \ > + echo " looks good."; \ > + fi > =20 > $(TEST_DIR)/boot_rom.bin: $(TEST_DIR)/boot_rom.elf > dd if=3D/dev/zero of=3D$@ bs=3D256 count=3D1 > @@ -60,4 +67,4 @@ generated_files =3D $(asm-offsets) > =20 > test_cases: $(generated_files) $(tests-common) $(tests) > =20 > -$(TEST_DIR)/selftest.elf: $(cstart.o) $(TEST_DIR)/selftest.o > +$(TEST_DIR)/selftest.elf: $(cstart.o) $(reloc.o) $(TEST_DIR)/selftest.o > diff --git a/powerpc/Makefile.ppc64 b/powerpc/Makefile.ppc64 > index 95bf6772d65ed..e965627991916 100644 > --- a/powerpc/Makefile.ppc64 > +++ b/powerpc/Makefile.ppc64 > @@ -8,6 +8,7 @@ ldarch =3D elf64-powerpc > kernel_offset =3D 0x0 > =20 > cstart.o =3D $(TEST_DIR)/cstart64.o > +reloc.o =3D $(TEST_DIR)/reloc64.o > cflatobjs +=3D lib/ppc64/spinlock.o > =20 > # ppc64 specific tests > diff --git a/powerpc/cstart64.S b/powerpc/cstart64.S > index 6d47eb59bb53e..d1a5531eb1c44 100644 > --- a/powerpc/cstart64.S > +++ b/powerpc/cstart64.S > @@ -24,12 +24,33 @@ > */ > .globl start > start: > - LOAD_REG_IMMEDIATE(r1, stackptr) > - LOAD_REG_IMMEDIATE(r2, tocptr) > + /* > + * We were loaded at QEMU's kernel load address, but we're not > + * allowed to link there due to how QEMU deals with linker VMAs, > + * so we just linked at zero. This means the first thing to do is > + * to find our stack and toc, and then do a relocate. > + */ > + bl 0f > +0: mflr r31 > + subi r31, r31, 0b - start /* QEMU's kernel load address */ > + ld r1, (p_stack - start)(r31) > + ld r2, (p_toc - start)(r31) > + add r1, r1, r31 > + add r2, r2, r31 > =20 > /* save DTB pointer */ > std r3, 56(r1) > =20 > + /* > + * Call relocate. relocate is C code, but careful to not use > + * any global references, as they may use absolute addresses, > + * which are, obviously, not yet relocated. > + */ > + mr r3, r31 > + ld r4, (p_dyn - start)(r31) > + add r4, r4, r31 > + bl relocate > + > bl hcall_patch_broken_sc1 > =20 > /* complete setup */ > @@ -37,13 +58,18 @@ start: > bl setup > =20 > /* run the test */ > - LOAD_REG_IMMEDIATE(r5, __argc) > - LOAD_REG_IMMEDIATE(r4, __argv) > + LOAD_REG_ADDR(r5, __argc) > + LOAD_REG_ADDR(r4, __argv) > lwz r3, 0(r5) > bl main > bl exit > b halt > =20 > +.align 3 > +p_stack: .llong stackptr > +p_toc: .llong tocptr > +p_dyn: .llong dynamic_start > + > .text > .align 3 > =20 > diff --git a/powerpc/flat.lds b/powerpc/flat.lds > index 84087057c0ce2..53221e8b4211c 100644 > --- a/powerpc/flat.lds > +++ b/powerpc/flat.lds > @@ -6,11 +6,22 @@ SECTIONS > etext =3D .; > .opd : { *(.opd) } > . =3D ALIGN(16); > + .dynamic : { > + dynamic_start =3D .; > + *(.dynamic) > + } > + .dynsym : { > + dynsym_start =3D .; > + *(.dynsym) > + } > + .rela.dyn : { *(.rela*) } > + . =3D ALIGN(16); > .data : { > *(.data) > + *(.data.rel*) > } > . =3D ALIGN(16); > - .rodata : { *(.rodata) } > + .rodata : { *(.rodata) *(.rodata.*) } > . =3D ALIGN(16); > .bss : { *(.bss) } > . =3D ALIGN(256); > diff --git a/powerpc/reloc64.c b/powerpc/reloc64.c > new file mode 100644 > index 0000000000000..d919372bf9288 > --- /dev/null > +++ b/powerpc/reloc64.c > @@ -0,0 +1,55 @@ > +/* > + * relocate R_PPC_RELATIVE RELA entries. Normally this is done in > + * assembly code to avoid the risk of using absolute addresses before > + * they're relocated. We use C, but cautiously (no global references). > + * > + * Copyright (C) 2016, Red Hat Inc, Andrew Jones > + * > + * This work is licensed under the terms of the GNU LGPL, version 2. > + */ > +#define DT_NULL 0 > +#define DT_RELA 7 > +#define DT_RELACOUNT 0x6ffffff9 > +#define R_PPC_RELATIVE 22 > + > +struct elf64_dyn { > + signed long long tag; > + unsigned long long val; > +}; > + > +#define RELA_GET_TYPE(rela_ptr) ((rela_ptr)->info & 0xffffffff) > +struct elf64_rela { > + unsigned long long offset; > + unsigned long long info; > + signed long long addend; > +}; > + > +void relocate(unsigned long load_addr, struct elf64_dyn *dyn_table) > +{ > + unsigned long long rela_addr =3D 0, rela_count =3D 0, *addr; > + struct elf64_dyn *d =3D dyn_table; > + struct elf64_rela *r; > + > + while (d && d->tag !=3D DT_NULL) { > + if (d->tag =3D=3D DT_RELA) > + rela_addr =3D d->val; > + else if (d->tag =3D=3D DT_RELACOUNT) > + rela_count =3D d->val; > + if (rela_addr && rela_count) > + break; > + ++d; > + } > + > + if (!rela_addr || !rela_count) > + return; > + > + r =3D (void *)(rela_addr + load_addr); > + > + while (rela_count--) { > + if (RELA_GET_TYPE(r) =3D=3D R_PPC_RELATIVE) { > + addr =3D (void *)(r->offset + load_addr); > + *addr =3D r->addend + load_addr; > + } > + ++r; > + } > +} --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --z+pzSjdB7cqptWpS Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJWw9dtAAoJEGw4ysog2bOSO1QP/ir8bp7K+QUn9LdmPmSRDxvr 75e9uhORfHTWmWl3Vx3Hm18fFdbcRWwBqoQg10aM97A9coIPOFx8ZUg0spOPbXUm q6qLgwJ4AZy2rjz1m8mhR8WSYmdOjjzPnk4N2S1psMoOTfrQh75hg+d68aS7Rfn+ bPlJp2Ndir8PafxIC1bDDFHbbClYDP5f7h93SuaTG0FeHz/vwadLpKBYgW+7h0um IqEHkLRMUMdiEhx7P6rMkbTRcvKVpxdJFLwY1bJt+Mv4Vq7mmWOGZNUghBWfc9oe y7nIoeq+Dj4oGoJ3fgvpatXbQwhxl1iPF92ALoK/e7+BBQW0NlKkPF+7jhTy9Ji/ nZCGLqv2wEtJUzX+goOuKMfp+M90xwZSEmSykjkv2CpZJpPvEfRBmdwNwdYVM6Of dha463n32ekavtIkifjbjs50zoXvEJcgeORHW7Wgfzn9Mx7kcmDibKnhIWweQX7X 3eiakuaygC/dRlUrZ6kPwax8fdRx9TTaHX15TozNBrnsL8t9hBbRJp3lyLGmNadS 8HlMAEUJjmqyQgzNCTvEj64ZHYQjGMyb9CDoVaW9NQI9wcHjuqzsDDKwMOm+8Cr9 rhxqpD6lKN1FOVL6TxdyIe3+fJADlyxrBb5vaG3P+gTikh+Uuj0BHVE3MRquvdKw ia4n5TVFqb2jQq318QWL =fqJt -----END PGP SIGNATURE----- --z+pzSjdB7cqptWpS--