From f2af6f7bf146ad0dedb26ae20a53b9743a5fbe5c Mon Sep 17 00:00:00 2001 From: Andrew Turner Date: Fri, 7 Oct 2016 16:17:51 +0000 Subject: [PATCH] The bootloader self relocation code was slightly wrong for the R_AARCH64_RELATIVE relocation found on arm64. It would try to add the contents of the memory location being relocated to the base address and the relocation addend. This worked when the contents was zero, however this now seems to be set to the value of the addend so we add this twice. Fix this by just setting the memory to the computed value. Reviewed by: emaste MFC after: 1 week Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D8177 --- sys/boot/common/self_reloc.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/sys/boot/common/self_reloc.c b/sys/boot/common/self_reloc.c index 29b9c5fabf0..081f38a28d8 100644 --- a/sys/boot/common/self_reloc.c +++ b/sys/boot/common/self_reloc.c @@ -108,12 +108,18 @@ self_reloc(Elf_Addr baseaddr, ElfW_Dyn *dynamic) break; case RELOC_TYPE_RELATIVE: - /* Address relative to the base address. */ newaddr = (Elf_Addr *)(rel->r_offset + baseaddr); - *newaddr += baseaddr; - /* Add the addend when the ABI uses them */ #ifdef ELF_RELA - *newaddr += rel->r_addend; + /* + * For R_AARCH64_RELATIVE we need to calculate the + * delta between the address we are run from and the + * address we are linked at. As the latter is 0 we + * just use the address we are run from for this. + */ + *newaddr = baseaddr + rel->r_addend; +#else + /* Address relative to the base address. */ + *newaddr += baseaddr; #endif break; default: