Skip to contentSkip to conversation

riscv: fix computation of pc-relative relocation offset

PATCH In review Andreas Schwab 3 participants 3 messages Jun 26, 2019 – Jul 11, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: fix computation of pc-relative relocation offset

The offset calculation was missing the relocation addend.

---
 util/grub-mkimagexx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
util/grub-mkimagexx.c +2 -2
index bc087c2b5..d16ec63a1 100644
@@ -1232,8 +1232,7 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd,
 		 grub_uint32_t *t32 = (grub_uint32_t *) target;
 		 grub_uint16_t *t16 = (grub_uint16_t *) target;
 		 grub_uint8_t *t8 = (grub_uint8_t *) target;
-		 grub_int64_t off = (long)sym_addr - target_section_addr - offset
-				    - image_target->vaddr_offset;
+		 grub_int64_t off;
 
 		 /*
 		  * Instructions and instruction encoding are documented in the RISC-V
@@ -1243,6 +1242,7 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd,
 		  */
 
 		 sym_addr += addend;
+		 off = sym_addr - target_section_addr - offset - image_target->vaddr_offset;
 
 		 switch (ELF_R_TYPE (info))
 		   {
Signature · 6 lines
2.22.0

-- 
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
Tested-by on v1 1/1
#1 lore raw
Quoted 22 lines · Andreas Schwab

The offset calculation was missing the relocation addend.

---
 util/grub-mkimagexx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
util/grub-mkimagexx.c +1 -2
index bc087c2b5..d16ec63a1 100644
@@ -1232,8 +1232,7 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd,
 		 grub_uint32_t *t32 = (grub_uint32_t *) target;
 		 grub_uint16_t *t16 = (grub_uint16_t *) target;
 		 grub_uint8_t *t8 = (grub_uint8_t *) target;
-		 grub_int64_t off = (long)sym_addr - target_section_addr - offset
-				    - image_target->vaddr_offset;
+		 grub_int64_t off;
 
 		 /*
 		  * Instructions and instruction encoding are documented in the RISC-V

util/grub-mkimagexx.c SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd, · line 1248

Quoted 7 lines
util/grub-mkimagexx.c +1 -0
@@ -1243,6 +1242,7 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd,
 		  */
 
 		 sym_addr += addend;
+		 off = sym_addr - target_section_addr - offset - image_target->vaddr_offset;
 
 		 switch (ELF_R_TYPE (info))
 		   {

This patch can fix a grub abort issue which randomly shows 'unaligned pointer' warnings on console. I tried this patch on QEMU v4.0.50 + grub-2.0.4-rc1.

Reviewed-by
#2 lore raw
Quoted 28 lines · Chester Lin
Quoted 27 lines · Andreas Schwab

The offset calculation was missing the relocation addend.

---
 util/grub-mkimagexx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
util/grub-mkimagexx.c +2 -2
index bc087c2b5..d16ec63a1 100644
@@ -1232,8 +1232,7 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd,
 		 grub_uint32_t *t32 = (grub_uint32_t *) target;
 		 grub_uint16_t *t16 = (grub_uint16_t *) target;
 		 grub_uint8_t *t8 = (grub_uint8_t *) target;
-		 grub_int64_t off = (long)sym_addr - target_section_addr - offset
-				    - image_target->vaddr_offset;
+		 grub_int64_t off;
 
 		 /*
 		  * Instructions and instruction encoding are documented in the RISC-V
@@ -1243,6 +1242,7 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd,
 		  */
 
 		 sym_addr += addend;
+		 off = sym_addr - target_section_addr - offset - image_target->vaddr_offset;
 
Quoted 7 lines

util/grub-mkimagexx.c SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd, · line 1248

Quoted 2 lines
util/grub-mkimagexx.c +0 -0
 		 switch (ELF_R_TYPE (info))
 		   {

This patch can fix a grub abort issue which randomly shows 'unaligned pointer' warnings on console. I tried this patch on QEMU v4.0.50 + grub-2.0.4-rc1.

Daniel