From 066242363458fbf87291daff9454b6f2ac9ebd8b Mon Sep 17 00:00:00 2001 From: Toomas Soome Date: Thu, 29 Dec 2016 09:10:45 +0200 Subject: [PATCH] 7704 loader: Fix EFI self relocation code for rela architectures Reviewed by: Andrew Stormont Reviewed by: Robert Mustacchi Approved by: Dan McDonald --- usr/src/boot/sys/boot/common/self_reloc.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/usr/src/boot/sys/boot/common/self_reloc.c b/usr/src/boot/sys/boot/common/self_reloc.c index 3b27b17c76..f82006078f 100644 --- a/usr/src/boot/sys/boot/common/self_reloc.c +++ b/usr/src/boot/sys/boot/common/self_reloc.c @@ -25,13 +25,12 @@ */ #include -__FBSDID("$FreeBSD$"); #include #include #include -#if defined(__aarch64__) +#if defined(__aarch64__) || defined(__amd64__) #define ElfW_Rel Elf64_Rela #define ElfW_Dyn Elf64_Dyn #define ELFW_R_TYPE ELF64_R_TYPE @@ -40,10 +39,6 @@ __FBSDID("$FreeBSD$"); #define ElfW_Rel Elf32_Rel #define ElfW_Dyn Elf32_Dyn #define ELFW_R_TYPE ELF32_R_TYPE -#elif defined(__amd64__) -#define ElfW_Rel Elf64_Rel -#define ElfW_Dyn Elf64_Dyn -#define ELFW_R_TYPE ELF64_R_TYPE #else #error architecture not supported #endif @@ -99,7 +94,9 @@ self_reloc(Elf_Addr baseaddr, ElfW_Dyn *dynamic) } /* - * Perform the actual relocation. + * Perform the actual relocation. We rely on the object having been + * linked at 0, so that the difference between the load and link + * address is the same as the load address. */ for (; relsz > 0; relsz -= relent) { switch (ELFW_R_TYPE(rel->r_info)) { @@ -108,12 +105,13 @@ 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; + /* Addend relative to the base address. */ + *newaddr = baseaddr + rel->r_addend; +#else + /* Address relative to the base address. */ + *newaddr += baseaddr; #endif break; default: -- 2.11.4.GIT