diff options
author | emaste <emaste@FreeBSD.org> | 2016-12-28 21:58:20 +0000 |
---|---|---|
committer | emaste <emaste@FreeBSD.org> | 2016-12-28 21:58:20 +0000 |
commit | 10caa4bfa55b3722600c4e354d10c5f361dbbf49 (patch) | |
tree | 78ab5ca1829bca7687cec8c8476573c23ef0c2f9 /sys/boot/common | |
parent | 7d3e7b7b86ede6f54865650cf6c1258a072e4427 (diff) | |
download | FreeBSD-src-10caa4bfa55b3722600c4e354d10c5f361dbbf49.zip FreeBSD-src-10caa4bfa55b3722600c4e354d10c5f361dbbf49.tar.gz |
Fix EFI self relocation code for rela architectures
MFC r306812 (andrew):
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.
MFC r309360: EFI loaders: parse rela relocations on amd64
Prior to this change the loader self relocation code interpreted amd64's
rela relocations as if they were rel relocations, discarding the addend.
This "works" because GNU ld 2.17.50 stores the addend value in both the
r_addend field of the relocation (as expected) and at the target of the
relocation.
Other linkers, and possibly other versions of GNU ld, won't have this
behaviour, so interpret the relocations correctly.
Diffstat (limited to 'sys/boot/common')
-rw-r--r-- | sys/boot/common/self_reloc.c | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/sys/boot/common/self_reloc.c b/sys/boot/common/self_reloc.c index 29b9c5f..5f6bfcb 100644 --- a/sys/boot/common/self_reloc.c +++ b/sys/boot/common/self_reloc.c @@ -31,7 +31,7 @@ __FBSDID("$FreeBSD$"); #include <elf.h> #include <bootstrap.h> -#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 +40,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 +95,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 +106,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: |