Skip to contentSkip to conversation

ARM relocation fixes

RFC PATCH In review Vladimir 'φ-coder/phcoder' Serbinenko 2 participants 26 messages Dec 1, 2013 – Dec 3, 2013

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 ARM relocation fixes

Current ARM relocation doesn't handle the cases when the relocation cant be satisfied directly (like thumb call over 1M of distance or jump24 to thumb mode. Attached patch adds missing tampoline and missing relocation handling to EFI code (it didn't allow to use ARM (no-Thumb) binary with EFI).
I couldn't test it on either arm-efi or ARM64

conf/Makefile.common +1 -1
index 8a71f13..152847c 100644
@@ -11,7 +11,7 @@ if COND_sparc64_ieee1275
   LDFLAGS_PLATFORM = -Wl,-melf64_sparc -mno-relax
 endif
 if COND_arm
-  CFLAGS_PLATFORM += -mthumb-interwork -mlong-calls
+  CFLAGS_PLATFORM += -mthumb-interwork
   CCASFLAGS_PLATFORM = -mthumb-interwork
   LDFLAGS_PLATFORM = -Wl,--wrap=__clear_cache
 endif
grub-core/kern/arm/dl.c +160 -37

237 lines not on this page · raw message

grub-core/kern/arm/dl_helper.c +72 -78

222 lines not on this page · raw message

grub-core/kern/arm64/dl.c +78 -41

178 lines not on this page · raw message

grub-core/kern/arm64/dl_helper.c +12 -27
index 6f99087..d213ab9 100644
@@ -25,46 +25,31 @@
 #include <grub/i18n.h>
 #include <grub/arm64/reloc.h>
 
-static grub_ssize_t
-sign_compress_offset (grub_ssize_t offset, int bitpos)
-{
-  return offset & ((1LL << (bitpos + 1)) - 1);
-}
-
 /*
  * grub_arm64_reloc_xxxx26():
  *
  * JUMP26/CALL26 relocations for B and BL instructions.
  */
 
-grub_err_t
-grub_arm64_reloc_xxxx26 (grub_uint32_t *place, Elf64_Addr adjust)
+int
+grub_arm_64_check_xxxx26_offset (grub_int64_t offset)
 {
-  grub_uint32_t insword, insmask;
-  grub_ssize_t offset;
   const grub_ssize_t offset_low = -(1 << 27), offset_high = (1 << 27) - 1;
 
-  insword = grub_le_to_cpu32 (*place);
-  insmask = 0xfc000000;
-
-  offset = adjust;
-#ifndef GRUB_UTIL
-  offset -= (grub_addr_t) place;
-#endif
-
   if ((offset < offset_low) || (offset > offset_high))
-    {
-      return grub_error (GRUB_ERR_BAD_MODULE,
-			 N_("CALL26 Relocation out of range"));
-    }
+    return 0;
+  return 1;
+}
+
+void
+grub_arm64_set_xxxx26_offset (grub_uint32_t *place, grub_int64_t offset)
+{
+  const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc000000);
 
   grub_dprintf ("dl", "  reloc_xxxx64 %p %c= 0x%llx\n",
 		place, offset > 0 ? '+' : '-',
 		offset < 0 ? (long long) -(unsigned long long) offset : offset);
 
-  offset = sign_compress_offset (offset, 27) >> 2;
-
-  *place = grub_cpu_to_le32 ((insword & insmask) | offset);
-
-  return GRUB_ERR_NONE;
+  *place &= insmask;
+  *place |= grub_cpu_to_le32 (offset >> 2) & ~insmask;
 }
grub-core/kern/dl.c +3 -3
index 1d68414..3b6e4cf 100644
@@ -229,7 +229,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
   unsigned i;
   Elf_Shdr *s;
   grub_size_t tsize = 0, talign = 1;
-#if defined (__ia64__) || defined (__powerpc__) || defined (__mips__)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined (__sparc__)
   grub_size_t tramp;
   grub_size_t got;
   grub_err_t err;
@@ -245,7 +245,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
 	talign = s->sh_addralign;
     }
 
-#if defined (__ia64__) || defined (__powerpc__) || defined (__mips__)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined (__sparc__)
   err = grub_arch_dl_get_tramp_got_size (e, &tramp, &got);
   if (err)
     return err;
@@ -314,7 +314,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
 	  mod->segment = seg;
 	}
     }
-#if defined (__ia64__) || defined (__powerpc__) || defined (__mips__)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined (__sparc__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
   ptr += tramp;
include/grub/arm/reloc.h +22 -3
index 50d070f..b938037 100644
@@ -20,8 +20,27 @@
 #define GRUB_ARM_RELOC_H 1
 
 grub_err_t grub_arm_reloc_abs32 (grub_uint32_t *addr, Elf32_Addr sym_addr);
-grub_err_t grub_arm_reloc_jump24 (grub_uint32_t *addr, Elf32_Addr sym_addr);
-grub_err_t grub_arm_reloc_thm_call (grub_uint16_t *addr, Elf32_Addr sym_addr);
-grub_err_t grub_arm_reloc_thm_jump19 (grub_uint16_t *addr, Elf32_Addr sym_addr);
+
+int
+grub_arm_thm_call_check_offset (grub_int32_t offset, int is_thumb);
+grub_int32_t
+grub_arm_thm_call_get_offset (grub_uint16_t *target);
+grub_err_t
+grub_arm_thm_call_set_offset (grub_uint16_t *target, grub_int32_t offset);
+
+grub_int32_t
+grub_arm_thm_jump19_get_offset (grub_uint16_t *target);
+void
+grub_arm_thm_jump19_set_offset (grub_uint16_t *target, grub_int32_t offset);
+int
+grub_arm_thm_jump19_check_offset (grub_int32_t offset);
+
+grub_int32_t
+grub_arm_jump24_get_offset (grub_uint32_t *target);
+int
+grub_arm_jump24_check_offset (grub_int32_t offset);
+void
+grub_arm_jump24_set_offset (grub_uint32_t *target,
+			    grub_int32_t offset);
 
 #endif
include/grub/arm64/reloc.h +3 -1
index 606d71c..4aed3d7 100644
@@ -19,6 +19,8 @@
 #ifndef GRUB_ARM64_RELOC_H
 #define GRUB_ARM64_RELOC_H 1
 
-grub_err_t grub_arm64_reloc_xxxx26 (grub_uint32_t *target, Elf64_Addr sym_addr);
+int grub_arm_64_check_xxxx26_offset (grub_int64_t offset);
+void
+grub_arm64_set_xxxx26_offset (grub_uint32_t *place, grub_int64_t offset);
 
 #endif
include/grub/dl.h +7 -2
index d1d20d9..263e839 100644
@@ -179,7 +179,7 @@ struct grub_dl
   Elf_Sym *symtab;
   void (*init) (struct grub_dl *mod);
   void (*fini) (void);
-#if defined (__ia64__) || defined (__powerpc__) || defined (__mips__)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined (__sparc__)
   void *got;
   void *tramp;
 #endif
@@ -256,11 +256,16 @@ grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
 				 grub_size_t *got);
 #endif
 
-#if defined (__powerpc__) || defined (__mips__)
+#if defined (__powerpc__) || defined (__mips__) || defined (__arm__)
 #define GRUB_ARCH_DL_TRAMP_ALIGN 4
 #define GRUB_ARCH_DL_GOT_ALIGN 4
 #endif
 
+#if defined (__aarch64__)
+#define GRUB_ARCH_DL_TRAMP_ALIGN 8
+#define GRUB_ARCH_DL_GOT_ALIGN 8
+#endif
+
 #endif
 
 #endif /* ! GRUB_DL_H */
util/grub-mkimagexx.c +25 -4
index 5240fe2..e2e598e 100644
@@ -731,13 +731,13 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr *sections,
 		   case R_AARCH64_JUMP26:
 		   case R_AARCH64_CALL26:
 		     {
-		       grub_err_t err;
 		       sym_addr -= offset;
 		       sym_addr -= SUFFIX (entry_point);
-		       err = grub_arm64_reloc_xxxx26((grub_uint32_t *)target,
+		       if (!grub_arm_64_check_xxxx26_offset (sym_addr))
+			 grub_util_error ("%s", _("CALL26 Relocation out of range"));
+
+		       grub_arm64_set_xxxx26_offset((grub_uint32_t *)target,
 						     sym_addr);
-		       if (err)
-			 grub_util_error ("%s", grub_errmsg);
 		     }
 		     break;
 		   default:
@@ -764,6 +764,11 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr *sections,
 		       *target = grub_host_to_target32 (grub_target_to_host32 (*target) + sym_addr);
 		     }
 		     break;
+		     /* Happens when compiled with -march=armv4.
+			Since currently we need at least armv5, keep bx as-is.
+		     */
+		   case R_ARM_V4BX:
+		     break;
 		   case R_ARM_THM_CALL:
 		   case R_ARM_THM_JUMP24:
 		     {
@@ -790,6 +795,20 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr *sections,
 			 grub_util_error ("%s", grub_errmsg);
 		     }
 		     break;
+
+		   case R_ARM_CALL:
+		   case R_ARM_JUMP24:
+		     {
+		       grub_err_t err;
+		       grub_util_info ("  JUMP24:\ttarget=0x%08lx\toffset=(0x%08x)",	(unsigned long) target, sym_addr);
+		       sym_addr -= offset;
+		       err = grub_arm_reloc_jump24 (target,
+						    sym_addr);
+		       if (err)
+			 grub_util_error ("%s", grub_errmsg);
+		     }
+		     break;
+
 		   default:
 		     grub_util_error (_("relocation 0x%x is not implemented yet!"), ELF_R_TYPE (info));
 		     break;
@@ -1054,11 +1073,13 @@ SUFFIX (make_reloc_section) (Elf_Ehdr *e, void **out,
 	      case EM_ARM:
 		switch (ELF_R_TYPE (info))
 		  {
+		  case R_ARM_V4BX:
 		    /* Relative relocations do not require fixup entries. */
 		  case R_ARM_JUMP24:
 		  case R_ARM_THM_CALL:
 		  case R_ARM_THM_JUMP19:
 		  case R_ARM_THM_JUMP24:
+		  case R_ARM_CALL:
 		    {
 		      Elf_Addr addr;
 
util/mkimage.c +88 -0

94 lines not on this page · raw message

#1 lore raw
Quoted 6 lines · Vladimir 'φ-coder/phcoder' Serbinenko

Current ARM relocation doesn't handle the cases when the relocation cant be satisfied directly (like thumb call over 1M of distance or jump24 to thumb mode. Attached patch adds missing tampoline and missing relocation handling to EFI code (it didn't allow to use ARM (no-Thumb) binary with EFI).
I couldn't test it on either arm-efi or ARM64

Amusingly, I wrote the attached on Saturday, based on a bug report from Jon Masters @ Red Hat. Although an unlikely corner case, it does probably need the addition of grub_arch_dl_get_tramp_got_size() from your implementation in order to ensure the "veneers"[1] don't end up in a heap region different to and too far away from the one the module is loaded into.

I'll have a look and a poke on both 32- and 64-bit stuff and respond..

I would say the modifications to grub-mkimage for arm64 are probably
unnessecary: AArch64 relative branch range is +-128MB, and I don't think we'll see grub kernel images that big.

/
Leif

[1]
ARM terminology - "trampolines" for ARM refers to something generated on the stack, which we don't see anymore since the nested functions were removed.

#2 lore raw

*cough*

With actual attachment.

Quoted 21 lines · Leif Lindholm
Quoted 6 lines · Vladimir 'φ-coder/phcoder' Serbinenko

Current ARM relocation doesn't handle the cases when the relocation cant be satisfied directly (like thumb call over 1M of distance or jump24 to thumb mode. Attached patch adds missing tampoline and missing relocation handling to EFI code (it didn't allow to use ARM (no-Thumb) binary with EFI).
I couldn't test it on either arm-efi or ARM64

Amusingly, I wrote the attached on Saturday, based on a bug report from Jon Masters @ Red Hat. Although an unlikely corner case, it does probably need the addition of grub_arch_dl_get_tramp_got_size() from your implementation in order to ensure the "veneers"[1] don't end up in a heap region different to and too far away from the one the module is loaded into.

I'll have a look and a poke on both 32- and 64-bit stuff and respond..

I would say the modifications to grub-mkimage for arm64 are probably
unnessecary: AArch64 relative branch range is +-128MB, and I don't think we'll see grub kernel images that big.

Quoted 7 lines

/
Leif

[1]
ARM terminology - "trampolines" for ARM refers to something generated on the stack, which we don't see anymore since the nested functions were removed.

From 8d7948641b864168acc67cf0a4834585b3242748 Mon Sep 17 00:00:00 2001
From: Leif Lindholm <[email protected]>
Date: Sat, 30 Nov 2013 13:05:34 +0000
Subject: [PATCH] arm64: dl: generate veneers for out-of-range relocations

---
 grub-core/kern/arm64/dl_helper.c |   45 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
grub-core/kern/arm64/dl_helper.c +45 -0
index ae4bce8..a473661 100644
@@ -17,6 +17,7 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <grub/cache.h>
 #include <grub/dl.h>
 #include <grub/elf.h>
 #include <grub/misc.h>
@@ -32,6 +33,46 @@ sign_compress_offset (grub_ssize_t offset, int bitpos)
 }
 
 /*
+ * AArch64 relative branch offset range is +-128MB.
+ * Modules are loaded onto the heap, which may be further away from the
+ * kernel than this. The workaround for this is in ARM-terminology called
+ * a "veneer" - a short sequence of a literal load, a branch to register
+ * and immediately following: the literal (64-bit aligned) 64-bit value.
+ */
+#ifndef GRUB_UTIL
+#define VENEER_LOAD 0x58000050 /* LDR  x16, [PC + 8] */
+#define VENEER_JUMP 0xd61f0200 /* BR   x16  */
+struct veneer {
+  grub_uint32_t load;
+  grub_uint32_t jump;
+  Elf64_Addr target;
+};
+
+static grub_err_t
+add_veneer (grub_uint32_t *place, Elf64_Addr adjust)
+{
+  struct veneer *current;
+  grub_err_t retval;
+
+  current = grub_malloc (sizeof(struct veneer));
+  if (!current)
+    return GRUB_ERR_OUT_OF_MEMORY;
+
+  current->load = VENEER_LOAD;
+  current->jump = VENEER_JUMP;
+  current->target = adjust;
+
+  retval = grub_arm64_reloc_xxxx26 (place, (Elf64_Addr) current);
+  if (retval == GRUB_ERR_NONE)
+    grub_arch_sync_caches (current, sizeof(struct veneer));
+  else
+    grub_free (current);
+
+  return retval;
+}
+#endif
+
+/*
  * grub_arm64_reloc_xxxx26():
  *
  * JUMP26/CALL26 relocations for B and BL instructions.
@@ -54,8 +95,12 @@ grub_arm64_reloc_xxxx26 (grub_uint32_t *place, Elf64_Addr adjust)
 
   if ((offset < offset_low) || (offset > offset_high))
     {
+#ifndef GRUB_UTIL
+      return add_veneer (place, adjust);
+#else
       return grub_error (GRUB_ERR_BAD_MODULE,
 			 N_("CALL26 Relocation out of range"));
+#endif
     }
 
   grub_dprintf ("dl", "  reloc_xxxx64 %p %c= 0x%llx\n",
Signature · 1 line
1.7.10.4
#3 lore raw
In reply to Leif Lindholm
Quoted 9 lines · Leif Lindholm
Quoted 6 lines · Vladimir 'φ-coder/phcoder' Serbinenko

Current ARM relocation doesn't handle the cases when the relocation cant be satisfied directly (like thumb call over 1M of distance or jump24 to thumb mode. Attached patch adds missing tampoline and missing relocation handling to EFI code (it didn't allow to use ARM (no-Thumb) binary with EFI).
I couldn't test it on either arm-efi or ARM64

Amusingly, I wrote the attached

Nothing is attached to your mail. Right now could you pause work on */dl.c: I'm reorganising them to declare more of it as platform independent and unify handling (ARM dl.c is unnecessarily different from other versions and forget some of ELF handling)

Quoted 7 lines

on Saturday, based on a bug report
from Jon Masters @ Red Hat. Although an unlikely corner case, it does probably need the addition of grub_arch_dl_get_tramp_got_size() from your implementation in order to ensure the "veneers"[1] don't end up in a heap region different to and too far away from the one the module is loaded into.

Quoted 5 lines

I'll have a look and a poke on both 32- and 64-bit stuff and respond..

I would say the modifications to grub-mkimage for arm64 are probably
unnessecary: AArch64 relative branch range is +-128MB, and I don't think we'll see grub kernel images that big.

There are no changes of this kind to mkimage. I only added missing ARM handling and the functions which now became mkimage-specific were moved to it.

Quoted 14 lines

/
Leif

[1]
ARM terminology - "trampolines" for ARM refers to something generated on the stack, which we don't see anymore since the nested functions were removed.

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw
Quoted 9 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 8 lines · Leif Lindholm
Quoted 6 lines · Vladimir 'φ-coder/phcoder' Serbinenko

Current ARM relocation doesn't handle the cases when the relocation cant be satisfied directly (like thumb call over 1M of distance or jump24 to thumb mode. Attached patch adds missing tampoline and missing relocation handling to EFI code (it didn't allow to use ARM (no-Thumb) binary with EFI).
I couldn't test it on either arm-efi or ARM64

Quoted 5 lines
Quoted 1 line

Amusingly, I wrote the attached

Nothing is attached to your mail. Right now could you pause work on */dl.c: I'm reorganising them to declare more of it as platform independent and unify handling (ARM dl.c is unnecessarily different from other versions and forget some of ELF handling)

Ok, I'll hold off.

Quoted 9 lines
Quoted 9 lines

on Saturday, based on a bug report
from Jon Masters @ Red Hat. Although an unlikely corner case, it does probably need the addition of grub_arch_dl_get_tramp_got_size() from your implementation in order to ensure the "veneers"[1] don't end up in a heap region different to and too far away from the one the module is loaded into.

I'll have a look and a poke on both 32- and 64-bit stuff and respond..

Quoted 7 lines
Quoted 3 lines

I would say the modifications to grub-mkimage for arm64 are probably
unnessecary: AArch64 relative branch range is +-128MB, and I don't think we'll see grub kernel images that big.

There are no changes of this kind to mkimage. I only added missing ARM handling and the functions which now became mkimage-specific were moved to it.

Well, I was referring specifically to:

util/grub-mkimagexx.c +4 -4
@@ -731,13 +731,13 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr *sections,
                   case R_AARCH64_JUMP26:
                   case R_AARCH64_CALL26:
                     {
-                      grub_err_t err;
                       sym_addr -= offset;
                       sym_addr -= SUFFIX (entry_point);
-                      err = grub_arm64_reloc_xxxx26((grub_uint32_t *)target,
+                      if (!grub_arm_64_check_xxxx26_offset (sym_addr))
+                        grub_util_error ("%s", _("CALL26 Relocation out of range"));
+
+                      grub_arm64_set_xxxx26_offset((grub_uint32_t *)target,
                                                     sym_addr);
-                      if (err)
-                        grub_util_error ("%s", grub_errmsg);
                     }
                     break;
                   default:

which isn't wrong, but that offset test can only fail when your GRUB kernel is greater than 128MB in size.

/
Leif

#5 lore raw
Quoted 56 lines · Leif Lindholm
Quoted 9 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 8 lines · Leif Lindholm
Quoted 6 lines · Vladimir 'φ-coder/phcoder' Serbinenko

Current ARM relocation doesn't handle the cases when the relocation cant be satisfied directly (like thumb call over 1M of distance or jump24 to thumb mode. Attached patch adds missing tampoline and missing relocation handling to EFI code (it didn't allow to use ARM (no-Thumb) binary with EFI).
I couldn't test it on either arm-efi or ARM64

Quoted 5 lines
Quoted 1 line

Amusingly, I wrote the attached

Nothing is attached to your mail. Right now could you pause work on */dl.c: I'm reorganising them to declare more of it as platform independent and unify handling (ARM dl.c is unnecessarily different from other versions and forget some of ELF handling)

Ok, I'll hold off.

Quoted 9 lines
Quoted 9 lines

on Saturday, based on a bug report
from Jon Masters @ Red Hat. Although an unlikely corner case, it does probably need the addition of grub_arch_dl_get_tramp_got_size() from your implementation in order to ensure the "veneers"[1] don't end up in a heap region different to and too far away from the one the module is loaded into.

I'll have a look and a poke on both 32- and 64-bit stuff and respond..

Quoted 7 lines
Quoted 3 lines

I would say the modifications to grub-mkimage for arm64 are probably
unnessecary: AArch64 relative branch range is +-128MB, and I don't think we'll see grub kernel images that big.

There are no changes of this kind to mkimage. I only added missing ARM handling and the functions which now became mkimage-specific were moved to it.

Well, I was referring specifically to:

util/grub-mkimagexx.c +4 -4
@@ -731,13 +731,13 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr *sections,
                   case R_AARCH64_JUMP26:
                   case R_AARCH64_CALL26:
                     {
-                      grub_err_t err;
                       sym_addr -= offset;
                       sym_addr -= SUFFIX (entry_point);
-                      err = grub_arm64_reloc_xxxx26((grub_uint32_t *)target,
+                      if (!grub_arm_64_check_xxxx26_offset (sym_addr))
+                        grub_util_error ("%s", _("CALL26 Relocation out of range"));
+
+                      grub_arm64_set_xxxx26_offset((grub_uint32_t *)target,
                                                     sym_addr);
-                      if (err)
-                        grub_util_error ("%s", grub_errmsg);
                     }
                     break;
                   default:
Quoted 3 lines

which isn't wrong, but that offset test can only fail when your GRUB kernel is greater than 128MB in size.

True that's why it just aborts the image creation.

Quoted 8 lines

/
Leif

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#6 lore raw
In reply to Leif Lindholm
Quoted 10 lines · Leif Lindholm
Quoted 9 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 8 lines · Leif Lindholm
Quoted 6 lines · Vladimir 'φ-coder/phcoder' Serbinenko

Current ARM relocation doesn't handle the cases when the relocation cant be satisfied directly (like thumb call over 1M of distance or jump24 to thumb mode. Attached patch adds missing tampoline and missing relocation handling to EFI code (it didn't allow to use ARM (no-Thumb) binary with EFI).
I couldn't test it on either arm-efi or ARM64

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Amusingly, I wrote the attached

Nothing is attached to your mail. Right now could you pause work on */dl.c: I'm reorganising them to declare more of it as platform independent and unify handling (ARM dl.c is unnecessarily different from other versions and forget some of ELF handling)

Ok, I'll hold off.

I've uploaded current work to phcoder/reloc

Quoted 49 lines
Quoted 9 lines
Quoted 9 lines

on Saturday, based on a bug report
from Jon Masters @ Red Hat. Although an unlikely corner case, it does probably need the addition of grub_arch_dl_get_tramp_got_size() from your implementation in order to ensure the "veneers"[1] don't end up in a heap region different to and too far away from the one the module is loaded into.

I'll have a look and a poke on both 32- and 64-bit stuff and respond..

Quoted 7 lines
Quoted 3 lines

I would say the modifications to grub-mkimage for arm64 are probably
unnessecary: AArch64 relative branch range is +-128MB, and I don't think we'll see grub kernel images that big.

There are no changes of this kind to mkimage. I only added missing ARM handling and the functions which now became mkimage-specific were moved to it.

Well, I was referring specifically to:

util/grub-mkimagexx.c +4 -4
@@ -731,13 +731,13 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr *sections,
                   case R_AARCH64_JUMP26:
                   case R_AARCH64_CALL26:
                     {
-                      grub_err_t err;
                       sym_addr -= offset;
                       sym_addr -= SUFFIX (entry_point);
-                      err = grub_arm64_reloc_xxxx26((grub_uint32_t *)target,
+                      if (!grub_arm_64_check_xxxx26_offset (sym_addr))
+                        grub_util_error ("%s", _("CALL26 Relocation out of range"));
+
+                      grub_arm64_set_xxxx26_offset((grub_uint32_t *)target,
                                                     sym_addr);
-                      if (err)
-                        grub_util_error ("%s", grub_errmsg);
                     }
                     break;
                   default:

which isn't wrong, but that offset test can only fail when your GRUB kernel is greater than 128MB in size.

/
Leif

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#7 lore raw
Quoted 9 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Amusingly, I wrote the attached

Nothing is attached to your mail. Right now could you pause work on */dl.c: I'm reorganising them to declare more of it as platform independent and unify handling (ARM dl.c is unnecessarily different from other versions and forget some of ELF handling)

Ok, I'll hold off.

I've uploaded current work to phcoder/reloc

Ok, so I've tested this on arm64, and it works on the commercial FVP Base model (which does not trigger veneer generation due to its runtime memory map), but crashes on the Foundation model (which does). So the generic dl refactoring seems correct, but somehing about the veneers is fishy.

My default ARMv7 UEFI build fails to grub-install with /work/local/grub/uefi/sbin/grub-install: error: bl/b.w targettting ARM.

I am investigating these issues.

/
Leif

#8 lore raw
Quoted 8 lines · Leif Lindholm
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Amusingly, I wrote the attached

Nothing is attached to your mail. Right now could you pause work on */dl.c: I'm reorganising them to declare more of it as platform independent and unify handling (ARM dl.c is unnecessarily different from other versions and forget some of ELF handling)

Ok, I'll hold off.

Quoted 7 lines
Quoted 1 line

I've uploaded current work to phcoder/reloc

Ok, so I've tested this on arm64, and it works on the commercial FVP Base model (which does not trigger veneer generation due to its runtime memory map), but crashes on the Foundation model (which does). So the generic dl refactoring seems correct, but somehing about the veneers is fishy.

Well, this one seems to be because mod->trampptr is never initilised (which should be a problem also on the other archs?).

With

grub-core/kern/dl.c +1 -0
index 33ccc98..92027e0 100644
@@ -317,6 +317,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
 #if !defined (__i386__) && !defined (__x86_64__) && !defined (__sparc__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
+  mod->trampptr = (ptr + tramp);
   ptr += tramp;
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_GOT_ALIGN);
   mod->got = ptr;

This runs successfully on my foundation model.

Presumably, on other architectures a similar thing for mod->gotptr is required?

/
Leif

#9 lore raw
Quoted 34 lines · Leif Lindholm
Quoted 8 lines · Leif Lindholm
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Amusingly, I wrote the attached

Nothing is attached to your mail. Right now could you pause work on */dl.c: I'm reorganising them to declare more of it as platform independent and unify handling (ARM dl.c is unnecessarily different from other versions and forget some of ELF handling)

Ok, I'll hold off.

Quoted 7 lines
Quoted 1 line

I've uploaded current work to phcoder/reloc

Ok, so I've tested this on arm64, and it works on the commercial FVP Base model (which does not trigger veneer generation due to its runtime memory map), but crashes on the Foundation model (which does). So the generic dl refactoring seems correct, but somehing about the veneers is fishy.

Well, this one seems to be because mod->trampptr is never initilised (which should be a problem also on the other archs?).

With

grub-core/kern/dl.c +1 -0
index 33ccc98..92027e0 100644
@@ -317,6 +317,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
 #if !defined (__i386__) && !defined (__x86_64__) && !defined (__sparc__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
+  mod->trampptr = (ptr + tramp);
   ptr += tramp;
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_GOT_ALIGN);
   mod->got = ptr;
Quoted 1 line

This runs successfully on my foundation model.

Umm, that's obviously not correct. But it did work :)

What is needed is to store the size of the module before adding trampoline and got sizes.

/
Leif

#10 lore raw
Quoted 23 lines · Leif Lindholm
Quoted 23 lines
Quoted 5 lines

Ok, so I've tested this on arm64, and it works on the commercial FVP Base model (which does not trigger veneer generation due to its runtime memory map), but crashes on the Foundation model (which does). So the generic dl refactoring seems correct, but somehing about the veneers is fishy.

Well, this one seems to be because mod->trampptr is never initilised (which should be a problem also on the other archs?).

With

grub-core/kern/dl.c +1 -0
index 33ccc98..92027e0 100644
@@ -317,6 +317,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
 #if !defined (__i386__) && !defined (__x86_64__) && !defined (__sparc__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
+  mod->trampptr = (ptr + tramp);
   ptr += tramp;
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_GOT_ALIGN);
   mod->got = ptr;
Quoted 6 lines
Quoted 1 line

This runs successfully on my foundation model.

Umm, that's obviously not correct. But it did work :)

What is needed is to store the size of the module before adding trampoline and got sizes.

So - after some much needed Lunch, and coffee, I think the below would be correct?

grub-core/kern/dl.c +2 -2
index 33ccc98..a83b744 100644
@@ -316,10 +316,10 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
     }
 #if !defined (__i386__) && !defined (__x86_64__) && !defined (__sparc__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
-  mod->tramp = ptr;
+  mod->tramp = mod->trampptr = ptr;
   ptr += tramp;
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_GOT_ALIGN);
-  mod->got = ptr;
+  mod->got = mod->gotptr = ptr;
   ptr += got;
 #endif
 

/
Leif

#11 lore raw
Quoted 41 lines · Leif Lindholm
Quoted 23 lines · Leif Lindholm
Quoted 23 lines
Quoted 5 lines

Ok, so I've tested this on arm64, and it works on the commercial FVP Base model (which does not trigger veneer generation due to its runtime memory map), but crashes on the Foundation model (which does). So the generic dl refactoring seems correct, but somehing about the veneers is fishy.

Well, this one seems to be because mod->trampptr is never initilised (which should be a problem also on the other archs?).

With

grub-core/kern/dl.c +1 -0
index 33ccc98..92027e0 100644
@@ -317,6 +317,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
 #if !defined (__i386__) && !defined (__x86_64__) && !defined (__sparc__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
+  mod->trampptr = (ptr + tramp);
   ptr += tramp;
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_GOT_ALIGN);
   mod->got = ptr;
Quoted 6 lines
Quoted 1 line

This runs successfully on my foundation model.

Umm, that's obviously not correct. But it did work :)

What is needed is to store the size of the module before adding trampoline and got sizes.

So - after some much needed Lunch, and coffee, I think the below would be correct?

grub-core/kern/dl.c +0 -0
index 33ccc98..a83b744 100644
@@ -316,10 +316,10 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
     }
 #if !defined (__i386__) && !defined (__x86_64__) && !defined (__sparc__)

grub-core/kern/dl.c grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e) · line 325

Quoted 10 lines
grub-core/kern/dl.c +2 -2
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
-  mod->tramp = ptr;
+  mod->tramp = mod->trampptr = ptr;
   ptr += tramp;
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_GOT_ALIGN);
-  mod->got = ptr;
+  mod->got = mod->gotptr = ptr;
   ptr += got;
 #endif
 

I've committed a fix before I saw this message but it did essentially the same.

Quoted 8 lines

/
Leif

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#12 lore raw
In reply to Leif Lindholm
Quoted 11 lines · Leif Lindholm
Quoted 10 lines · Leif Lindholm
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Amusingly, I wrote the attached

Nothing is attached to your mail. Right now could you pause work on */dl.c: I'm reorganising them to declare more of it as platform independent and unify handling (ARM dl.c is unnecessarily different from other versions and forget some of ELF handling)

Ok, I'll hold off.

I've uploaded current work to phcoder/reloc

Quoted 9 lines
Quoted 5 lines

Ok, so I've tested this on arm64, and it works on the commercial FVP Base model (which does not trigger veneer generation due to its runtime memory map), but crashes on the Foundation model (which does). So the generic dl refactoring seems correct, but somehing about the veneers is fishy.

Well, this one seems to be because mod->trampptr is never initilised (which should be a problem also on the other archs?).

Thanks, good catch, fixed, well it's my WIP branch.

Quoted 28 lines

With

grub-core/kern/dl.c +1 -0
index 33ccc98..92027e0 100644
@@ -317,6 +317,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
 #if !defined (__i386__) && !defined (__x86_64__) && !defined (__sparc__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
+  mod->trampptr = (ptr + tramp);
   ptr += tramp;
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_GOT_ALIGN);
   mod->got = ptr;

This runs successfully on my foundation model.

Presumably, on other architectures a similar thing for mod->gotptr is required?

/
Leif

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
.

#13 lore raw
In reply to Leif Lindholm
Quoted 2 lines · Leif Lindholm

My default ARMv7 UEFI build fails to grub-install with /work/local/grub/uefi/sbin/grub-install: error: bl/b.w targettting ARM.

This is a problem because of asm functions which are always ARM and gcc uses bl to jump to them from thumb that an't be satisified. I wonder if it's better to add veneers to mkimage or to add explicit thumb interwork to all asm functions like I did in my other patch. In my local build I've cheated by decraling all those functions with __attribute__ ((long_call)) but it's probably not right solution so I didn't commit it.

#14 lore raw
Quoted 7 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 2 lines

My default ARMv7 UEFI build fails to grub-install with /work/local/grub/uefi/sbin/grub-install: error: bl/b.w targettting ARM.

This is a problem because of asm functions which are always ARM and gcc uses bl to jump to them from thumb that an't be satisified. I wonder if it's better to add veneers to mkimage or to add explicit thumb interwork to all asm functions like I did in my other patch.

Ah. When linking with a standalone linker, it rewrites BL to BLX where this is required for state change - so doing that #ifdef GRUB_UTIL wouldn't be wrong.

Quoted 3 lines

In my local build
I've cheated by decraling all those functions with __attribute__ ((long_call)) but it's probably not right solution so I didn't commit it.

On a lazy note, putting -mlong-calls back into CFLAGS_PLATFORM resolves this issue (since it forces full address loads into PC rather than using relative addressing and unconditional instruction set state change).

/
Leif

#15 lore raw
Quoted 11 lines · Leif Lindholm
Quoted 7 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 2 lines

My default ARMv7 UEFI build fails to grub-install with /work/local/grub/uefi/sbin/grub-install: error: bl/b.w targettting ARM.

This is a problem because of asm functions which are always ARM and gcc uses bl to jump to them from thumb that an't be satisified. I wonder if it's better to add veneers to mkimage or to add explicit thumb interwork to all asm functions like I did in my other patch.

Ah. When linking with a standalone linker, it rewrites BL to BLX where this is required for state change

Doesn't this require Thumb2 ?

#16 lore raw
Quoted 8 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

My default ARMv7 UEFI build fails to grub-install with /work/local/grub/uefi/sbin/grub-install: error: bl/b.w targettting ARM.

This is a problem because of asm functions which are always ARM and gcc uses bl to jump to them from thumb that an't be satisified. I wonder if it's better to add veneers to mkimage or to add explicit thumb interwork to all asm functions like I did in my other patch.

Quoted 4 lines
Quoted 2 lines

Ah. When linking with a standalone linker, it rewrites BL to BLX where this is required for state change

Doesn't this require Thumb2 ?

No, BLX immediate has been supported since ARMv5T. (Yes, that is effectively a 32-bit encoding in what is a 16-bit instruction set - but so is the basic BL.)

/
Leif

#17 lore raw
Quoted 9 lines · Leif Lindholm
Quoted 8 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

My default ARMv7 UEFI build fails to grub-install with /work/local/grub/uefi/sbin/grub-install: error: bl/b.w targettting ARM.

This is a problem because of asm functions which are always ARM and gcc uses bl to jump to them from thumb that an't be satisified. I wonder if it's better to add veneers to mkimage or to add explicit thumb interwork to all asm functions like I did in my other patch.

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Ah. When linking with a standalone linker, it rewrites BL to BLX where this is required for state change

Doesn't this require Thumb2 ?

No, BLX immediate has been supported since ARMv5T. (Yes, that is effectively a 32-bit encoding in what is a 16-bit instruction set - but so is the basic BL.)

I've looked through encoding of those instructions and see how much it's a mess. b* and b*x don't have similar set of options which makes validating them a difficult error-prone task. So I think, I'll just add veneers to mkimage, just like we do on ia64 (either by making a pc-relative variant of veneers or adding fixup for them)

Quoted 8 lines

/
Leif

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#18 lore raw
Quoted 9 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 4 lines

No, BLX immediate has been supported since ARMv5T. (Yes, that is effectively a 32-bit encoding in what is a 16-bit instruction set - but so is the basic BL.)

I've looked through encoding of those instructions and see how much it's a mess. b* and b*x don't have similar set of options which makes validating them a difficult error-prone task. So I think, I'll just add veneers to mkimage, just like we do on ia64 (either by making a pc-relative variant of veneers or adding fixup for them)

Not B, BL.
There is a 1-bit range difference between Thumb BL and BLX, which we need to check for anyway. This check already exists (and must exist) in the code. Adding veneers would be pure overhead.

/
Leif

#19 lore raw
Quoted 11 lines · Leif Lindholm
Quoted 9 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 4 lines

No, BLX immediate has been supported since ARMv5T. (Yes, that is effectively a 32-bit encoding in what is a 16-bit instruction set - but so is the basic BL.)

I've looked through encoding of those instructions and see how much it's a mess. b* and b*x don't have similar set of options which makes validating them a difficult error-prone task. So I think, I'll just add veneers to mkimage, just like we do on ia64 (either by making a pc-relative variant of veneers or adding fixup for them)

Quoted 5 lines

Not B, BL.
There is a 1-bit range difference between Thumb BL and BLX, which we need to check for anyway. This check already exists (and must exist) in the code. Adding veneers would be pure overhead.

I meant that you can use conditions with bl but not blx. So if we have a reloc on ARM bl.e targetting Thumb then we have to add veneers. Since we have only small number of interworking calls it's probably easier to always add veneers on interworking relative relocations rather than having micro-optimisation and get some minor case wrong.

Quoted 8 lines

/
Leif

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#20 lore raw
Quoted 11 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 10 lines
Quoted 5 lines

I've looked through encoding of those instructions and see how much it's a mess. b* and b*x don't have similar set of options which makes validating them a difficult error-prone task. So I think, I'll just add veneers to mkimage, just like we do on ia64 (either by making a pc-relative variant of veneers or adding fixup for them)

Not B, BL.
There is a 1-bit range difference between Thumb BL and BLX, which we need to check for anyway. This check already exists (and must exist) in the code. Adding veneers would be pure overhead.

Quoted 5 lines

I meant that you can use conditions with bl but not blx. So if we have a reloc on ARM bl.e targetting Thumb then we have to add veneers. Since we have only small number of interworking calls it's probably easier to always add veneers on interworking relative relocations rather than having micro-optimisation and get some minor case wrong.

OK, but the only place we could ever have a problem with this would be if we had asm in the kernel _explicitly_ done as .thumb. Which we don't. We explicitly moved away from that in order to have support for pre-v7 processors.

All modules will have full 32-bit external references, so will not use these instructions anyway. Any internal references within modules will be linked with LD, which will fix this up automatically.

/
Leif

#21 lore raw
Quoted 18 lines · Leif Lindholm
Quoted 11 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 10 lines
Quoted 5 lines

I've looked through encoding of those instructions and see how much it's a mess. b* and b*x don't have similar set of options which makes validating them a difficult error-prone task. So I think, I'll just add veneers to mkimage, just like we do on ia64 (either by making a pc-relative variant of veneers or adding fixup for them)

Not B, BL.
There is a 1-bit range difference between Thumb BL and BLX, which we need to check for anyway. This check already exists (and must exist) in the code. Adding veneers would be pure overhead.

Quoted 5 lines

I meant that you can use conditions with bl but not blx. So if we have a reloc on ARM bl.e targetting Thumb then we have to add veneers. Since we have only small number of interworking calls it's probably easier to always add veneers on interworking relative relocations rather than having micro-optimisation and get some minor case wrong.

Quoted 5 lines

OK, but the only place we could ever have a problem with this would be if we had asm in the kernel _explicitly_ done as .thumb. Which we don't. We explicitly moved away from that in order to have support for pre-v7 processors.

We also call C code from asm. One such instance (for division instructions) caused the problem

Quoted 4 lines

All modules will have full 32-bit external references, so will not use these instructions anyway. Any internal references within modules will be linked with LD, which will fix this up automatically.

In my small test I compiled:
extern void g(void);

void f (int x)
{
if (!x)

    g();
}

And got following assembly with -Os:

0: e3500000 cmp r0, #0
4: e92d4008 push {r3, lr}
8: 0bfffffe bleq 0 <g>

			8: R_ARM_JUMP24	g

c: e8bd4008 pop {r3, lr}
10: e12fff1e bx lr

If g is a function in thumb kernel or thumb module then you need a veneer.

Quoted 8 lines

/
Leif

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#22 lore raw
Quoted 27 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 11 lines
Quoted 5 lines

I meant that you can use conditions with bl but not blx. So if we have a reloc on ARM bl.e targetting Thumb then we have to add veneers. Since we have only small number of interworking calls it's probably easier to always add veneers on interworking relative relocations rather than having micro-optimisation and get some minor case wrong.

OK, but the only place we could ever have a problem with this would be if we had asm in the kernel _explicitly_ done as .thumb. Which we don't. We explicitly moved away from that in order to have support for pre-v7 processors.

We also call C code from asm. One such instance (for division instructions) caused the problem

Quoted 4 lines

All modules will have full 32-bit external references, so will not use these instructions anyway. Any internal references within modules will be linked with LD, which will fix this up automatically.

In my small test I compiled:
extern void g(void);

void f (int x)
{
if (!x)

    g();
}

And got following assembly with -Os:

Quoted 8 lines
   0:	e3500000 	cmp	r0, #0
   4:	e92d4008 	push	{r3, lr}
   8:	0bfffffe 	bleq	0 <g>
			8: R_ARM_JUMP24	g
   c:	e8bd4008 	pop	{r3, lr}

10: e12fff1e bx lr

If g is a function in thumb kernel or thumb module then you need a veneer.

Ok, you got me. Didn't consider -Os.
But the second case would still be auto-added by the linker.

But what is the objection to -mlong-calls?

My armv7 kernel ends up only slightly larger with this option (57272 bytes vs. 57088) - 184 bytes, from which 12 bytes per veneer can be subtracted. And the overall arm-efi directory is smaller (10031244 vs. 10254924). For just the *.mod too (1229498 vs. 1234034).

When compiling for For ARM (A32) (i.e. armv6), there is no difference in kernel size, but modules do grow 1.8% from 1477726 to 1503986. But is it really worth adding complexity to grub-mkimage for a small benefit to legacy platforms only? Could we instead add an arm_cflags with -mlong-calls for kernel in Makefile.core.def?

/
Leif

#23 lore raw
Quoted 35 lines · Leif Lindholm
Quoted 34 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 11 lines
Quoted 5 lines

I meant that you can use conditions with bl but not blx. So if we have a reloc on ARM bl.e targetting Thumb then we have to add veneers. Since we have only small number of interworking calls it's probably easier to always add veneers on interworking relative relocations rather than having micro-optimisation and get some minor case wrong.

OK, but the only place we could ever have a problem with this would be if we had asm in the kernel _explicitly_ done as .thumb. Which we don't. We explicitly moved away from that in order to have support for pre-v7 processors.

We also call C code from asm. One such instance (for division instructions) caused the problem

Quoted 4 lines

All modules will have full 32-bit external references, so will not use these instructions anyway. Any internal references within modules will be linked with LD, which will fix this up automatically.

In my small test I compiled:
extern void g(void);

void f (int x)
{
if (!x)

    g();
}

And got following assembly with -Os:

   0:	e3500000 	cmp	r0, #0
   4:	e92d4008 	push	{r3, lr}
   8:	0bfffffe 	bleq	0 <g>
			8: R_ARM_JUMP24	g
   c:	e8bd4008 	pop	{r3, lr}

10: e12fff1e bx lr

Quoted 5 lines
Quoted 1 line

If g is a function in thumb kernel or thumb module then you need a veneer.

Ok, you got me. Didn't consider -Os.
But the second case would still be auto-added by the linker.

LD in -r mode doesn't always resolve all relocs

Quoted 2 lines

But what is the objection to -mlong-calls?

Originally it was from my experiments with clang. It doesn't accept -mlong-calls. But clang isn't enough of motivation for this complexity, far from it. My motivation is to have a robust dynamic linker with interwork possibilities, that we won't have to rewrite when new compiler changes behaviour or if we decide to decrease the requirement to armv4. I think, I'll make build system add -mlong-calls if it's supported by compiler.

Quoted 7 lines

My armv7 kernel ends up only slightly larger with this option (57272 bytes vs. 57088) - 184 bytes, from which 12 bytes per veneer can be subtracted. And the overall arm-efi directory is smaller (10031244 vs. 10254924). For just the *.mod too (1229498 vs. 1234034).

When compiling for For ARM (A32) (i.e. armv6), there is no difference in kernel size, but modules do grow 1.8% from 1477726 to 1503986.

I'm confused by numbers: I don't see which ones relate to which configs (long-calls/no-long-calls A32/T16/T32)

Quoted 12 lines

But is it really worth adding complexity to grub-mkimage for a small benefit to legacy platforms only? Could we instead add an arm_cflags with -mlong-calls for kernel in Makefile.core.def?

/
Leif

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#24 lore raw
In reply to Leif Lindholm
Quoted 9 lines · Leif Lindholm
Quoted 8 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

My default ARMv7 UEFI build fails to grub-install with /work/local/grub/uefi/sbin/grub-install: error: bl/b.w targettting ARM.

This is a problem because of asm functions which are always ARM and gcc uses bl to jump to them from thumb that an't be satisified. I wonder if it's better to add veneers to mkimage or to add explicit thumb interwork to all asm functions like I did in my other patch.

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Ah. When linking with a standalone linker, it rewrites BL to BLX where this is required for state change

Doesn't this require Thumb2 ?

No, BLX immediate has been supported since ARMv5T. (Yes, that is effectively a 32-bit encoding in what is a 16-bit instruction set - but so is the basic BL.)

In my branch I added veneers to mkimage. Do you have a tutorial on setting up arm-efi VM?

Quoted 8 lines

/
Leif

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#25 lore raw
Quoted 11 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Ah. When linking with a standalone linker, it rewrites BL to BLX where this is required for state change

Doesn't this require Thumb2 ?

No, BLX immediate has been supported since ARMv5T. (Yes, that is effectively a 32-bit encoding in what is a 16-bit instruction set - but so is the basic BL.)

In my branch I added veneers to mkimage. Do you have a tutorial on setting up arm-efi VM?

It is slightly trickier for 32-bit ARM, in that we don't have a costless model available. There is a port to Qemu underway, but it is not entirely complete, and requires a patch or two in Qemu.

I can however verify that 559c2fe runs correctly on my TC2 hardware platform.

/
Leif