From: David Gibson <david@gibson.dropbear.id.au>
To: Andrew Jones <drjones@redhat.com>
Cc: kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, dgibson@redhat.com,
agraf@suse.de, thuth@redhat.com, lvivier@redhat.com,
pbonzini@redhat.com
Subject: Re: [kvm-unit-tests PATCH 10/14] powerpc/ppc64: relocate linker VMAs
Date: Tue, 04 Aug 2015 03:53:24 +0000 [thread overview]
Message-ID: <20150804035324.GC3080@voom.redhat.com> (raw)
In-Reply-To: <1438612891-3718-11-git-send-email-drjones@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 7603 bytes --]
On Mon, Aug 03, 2015 at 04:41:27PM +0200, 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 == LMA. Thus, we link with VMA == 0x0, and then deal with
> relocation.
>
> Signed-off-by: Andrew Jones <drjones@redhat.com>
> ---
> configure | 2 ++
> powerpc/Makefile.common | 13 +++++++++---
> powerpc/Makefile.ppc64 | 1 +
> powerpc/cstart64.S | 40 +++++++++++++++++++++++++++++++----
> powerpc/flat.lds | 13 +++++++++++-
> powerpc/reloc64.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++
> 6 files changed, 116 insertions(+), 8 deletions(-)
> create mode 100644 powerpc/reloc64.c
>
> diff --git a/configure b/configure
> index b367224093369..b2ad199da7873 100755
> --- a/configure
> +++ b/configure
> @@ -5,6 +5,7 @@ kerneldir=/lib/modules/$(uname -r)/build
> cc=gcc
> ld=ld
> objcopy=objcopy
> +objdump=objdump
> ar=ar
> arch=`uname -m | sed -e s/i.86/i386/ | sed -e 's/arm.*/arm/'`
> host=$arch
> @@ -132,6 +133,7 @@ PROCESSOR=$processor
> CC=$cross_prefix$cc
> LD=$cross_prefix$ld
> OBJCOPY=$cross_prefix$objcopy
> +OBJDUMP=$cross_prefix$objdump
> AR=$cross_prefix$ar
> API=$api
> TEST_DIR=$testdir
> diff --git a/powerpc/Makefile.common b/powerpc/Makefile.common
> index d6356540918a5..b130342dee60e 100644
> --- a/powerpc/Makefile.common
> +++ b/powerpc/Makefile.common
> @@ -27,6 +27,7 @@ CFLAGS += -Wextra
> CFLAGS += -O2
> CFLAGS += -I lib -I lib/libfdt
> CFLAGS += -Wa,-mregnames
> +CFLAGS += -fpie
>
> asm-offsets = lib/$(ARCH)/asm-offsets.h
> include scripts/asm-offsets.mak
> @@ -43,11 +44,17 @@ libgcc := $(shell $(CC) $(machine) --print-libgcc-file-name)
> start_addr := $(shell printf "%x\n" $$(( $(phys_base) + $(kernel_offset) )))
>
> FLATLIBS = $(libcflat) $(LIBFDT_archive) $(libgcc)
> -%.elf: LDFLAGS = $(CFLAGS) -nostdlib
> +%.elf: LDFLAGS = $(CFLAGS) -nostdlib -pie
> %.elf: %.o $(FLATLIBS) powerpc/flat.lds
> $(CC) $(LDFLAGS) -o $@ \
> -Wl,-T,powerpc/flat.lds,--build-id=none,-Ttext=$(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
>
> powerpc_clean: libfdt_clean asm_offsets_clean
> $(RM) $(TEST_DIR)/*.{o,elf} \
> @@ -59,5 +66,5 @@ generated_files = $(asm-offsets)
>
> test_cases: $(generated_files) $(tests-common) $(tests)
>
> -$(TEST_DIR)/$(TEST).elf: $(cstart.o) $(TEST_DIR)/$(TEST).o
> -$(TEST_DIR)/selftest.elf: $(cstart.o) $(TEST_DIR)/selftest.o
> +$(TEST_DIR)/$(TEST).elf: $(cstart.o) $(reloc.o) $(TEST_DIR)/$(TEST).o
> +$(TEST_DIR)/selftest.elf: $(cstart.o) $(reloc.o) $(TEST_DIR)/selftest.o
> diff --git a/powerpc/Makefile.ppc64 b/powerpc/Makefile.ppc64
> index 7c61933dfa8ba..7274e0d98b5a5 100644
> --- a/powerpc/Makefile.ppc64
> +++ b/powerpc/Makefile.ppc64
> @@ -8,6 +8,7 @@ ldarch = elf64-powerpc #elf64-powerpcle (eventually)
> kernel_offset = 0x0
>
> cstart.o = $(TEST_DIR)/cstart64.o
> +reloc.o = $(TEST_DIR)/reloc64.o
> cflatobjs += lib/ppc64/processor.o
> cflatobjs += lib/ppc64/spinlock.o
>
> diff --git a/powerpc/cstart64.S b/powerpc/cstart64.S
> index 141d4563563d5..8edaaa6e251fc 100644
> --- a/powerpc/cstart64.S
> +++ b/powerpc/cstart64.S
> @@ -26,18 +26,50 @@
> */
> .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 . + 4
"bl 0f" might make the connection to the following instructions
clearer.
> +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
> +
> + /* save DTB pointer */
> + std r3, 56(r1)
> +
> + /*
> + * 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
> +
> + /* complete setup */
> + ld r3, 56(r1)
> bl .setup
>
> /* 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
>
> +.align 3
> +p_stack: .llong stackptr
> +p_toc: .llong tocptr
> +p_dyn: .llong dynamic_start
> +
> .text
> .align 3
>
> diff --git a/powerpc/flat.lds b/powerpc/flat.lds
> index bd075efb2c51b..8a573d27346de 100644
> --- a/powerpc/flat.lds
> +++ b/powerpc/flat.lds
> @@ -6,11 +6,22 @@ SECTIONS
> etext = .;
> .opd : { *(.opd) }
> . = ALIGN(16);
> + .dynamic : {
> + dynamic_start = .;
> + *(.dynamic)
> + }
> + .dynsym : {
> + dynsym_start = .;
> + *(.dynsym)
> + }
> + .rela.dyn : { *(.rela*) }
> + . = ALIGN(16);
> .data : {
> *(.data)
> + *(.data.rel*)
> }
> . = ALIGN(16);
> - .rodata : { *(.rodata) }
> + .rodata : { *(.rodata) *(.rodata.*) }
> . = ALIGN(16);
> .bss : { *(.bss) }
> . = ALIGN(16);
> diff --git a/powerpc/reloc64.c b/powerpc/reloc64.c
> new file mode 100644
> index 0000000000000..2804823bdfee3
> --- /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) 2015, Red Hat Inc, Andrew Jones <drjones@redhat.com>
> + *
> + * 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 = 0, rela_count = 0, *addr;
> + struct elf64_dyn *d = dyn_table;
> + struct elf64_rela *r;
> +
> + while (d && d->tag != DT_NULL) {
> + if (d->tag == DT_RELA)
> + rela_addr = d->val;
> + else if (d->tag == DT_RELACOUNT)
> + rela_count = d->val;
> + if (rela_addr && rela_count)
> + break;
> + ++d;
> + }
> +
> + if (!rela_addr || !rela_count)
> + return;
> +
> + r = (void *)(rela_addr + load_addr);
> +
> + while (rela_count--) {
> + if (RELA_GET_TYPE(r) == R_PPC_RELATIVE) {
> + addr = (void *)(r->offset + load_addr);
> + *addr = r->addend + load_addr;
> + }
> + ++r;
> + }
> +}
--
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
[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]
next prev parent reply other threads:[~2015-08-04 3:53 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-08-03 14:41 [kvm-unit-tests PATCH 00/14] ppc64: initial drop Andrew Jones
2015-08-03 14:41 ` [kvm-unit-tests PATCH 01/14] lib: asm-generic: add missing casts Andrew Jones
2015-08-03 14:41 ` [kvm-unit-tests PATCH 02/14] lib: share arm-selftest utility functions Andrew Jones
2015-08-03 14:41 ` [kvm-unit-tests PATCH 03/14] config: no need to mix arch makefiles Andrew Jones
2015-08-03 14:41 ` [kvm-unit-tests PATCH 04/14] powerpc/ppc64: start skeleton framework Andrew Jones
2015-08-03 14:41 ` [kvm-unit-tests PATCH 05/14] powerpc/pp64: ppc-ify makefiles and linker script Andrew Jones
2015-08-03 14:41 ` [kvm-unit-tests PATCH 06/14] powerpc/ppc64: add boot rom source Andrew Jones
2015-08-03 14:41 ` [kvm-unit-tests PATCH 07/14] powerpc/ppc64: add bootloader to bounce into memory Andrew Jones
2015-08-03 14:41 ` [kvm-unit-tests PATCH 08/14] powerpc/ppc64: add HV putchar Andrew Jones
2015-08-04 3:50 ` David Gibson
2015-08-04 7:33 ` Andrew Jones
2015-08-03 14:41 ` [kvm-unit-tests PATCH 09/14] powerpc/ppc64: adapt arm's setup Andrew Jones
2015-08-03 14:41 ` [kvm-unit-tests PATCH 10/14] powerpc/ppc64: relocate linker VMAs Andrew Jones
2015-08-04 3:53 ` David Gibson [this message]
2015-08-04 7:35 ` Andrew Jones
2015-08-03 14:41 ` [kvm-unit-tests PATCH 11/14] powerpc/ppc64: add rtas_power_off Andrew Jones
2015-08-03 17:08 ` Paolo Bonzini
2015-08-03 18:02 ` Andrew Jones
2015-08-03 22:27 ` Alexander Graf
2015-08-04 4:09 ` David Gibson
2015-08-04 7:47 ` Andrew Jones
2015-08-04 13:15 ` Paolo Bonzini
2015-08-04 13:21 ` Andrew Jones
2015-08-05 0:06 ` David Gibson
2015-08-04 4:03 ` David Gibson
2015-08-04 7:51 ` Andrew Jones
2015-08-04 4:11 ` David Gibson
2015-08-04 7:54 ` Andrew Jones
2015-08-05 0:12 ` David Gibson
2015-08-03 14:41 ` [kvm-unit-tests PATCH 12/14] scripts: add exit code snooper Andrew Jones
2015-08-03 14:41 ` [kvm-unit-tests PATCH 13/14] powerpc/ppc64: add run script and unittests.cfg Andrew Jones
2015-08-03 14:41 ` [kvm-unit-tests PATCH 14/14] mkstandalone: add support for powerpc Andrew Jones
2015-08-03 17:06 ` [kvm-unit-tests PATCH 00/14] ppc64: initial drop Paolo Bonzini
2015-11-03 7:08 ` Thomas Huth
2015-11-03 9:40 ` Paolo Bonzini
2015-11-03 14:56 ` Andrew Jones
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=20150804035324.GC3080@voom.redhat.com \
--to=david@gibson.dropbear.id.au \
--cc=agraf@suse.de \
--cc=dgibson@redhat.com \
--cc=drjones@redhat.com \
--cc=kvm-ppc@vger.kernel.org \
--cc=kvm@vger.kernel.org \
--cc=lvivier@redhat.com \
--cc=pbonzini@redhat.com \
--cc=thuth@redhat.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