From 4ab5156c01bfd4a6304e26b1dc2d34163b8637c0 Mon Sep 17 00:00:00 2001
From: Jim Mussared <jim.mussared@gmail.com>
Date: Sat, 17 Aug 2019 00:32:04 +1000
Subject: [PATCH] tools/mpy-tool.py: Force native func alignment to
 halfword/word on ARM.

This is necessary for ARMV6 and V7.  Without this change, calling a frozen
native/viper function that is misaligned will crash.
---
 tools/mpy-tool.py | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py
index 648d56fe0..7938ea5dc 100755
--- a/tools/mpy-tool.py
+++ b/tools/mpy-tool.py
@@ -471,6 +471,14 @@ class RawCodeNative(RawCode):
         else:
             self.fun_data_attributes = '__attribute__((section(".text,\\"ax\\",%progbits @ ")))'
 
+        # Allow single-byte alignment by default for x86/x64/xtensa, but on ARM we need halfword- or word- alignment.
+        if config.native_arch == MP_NATIVE_ARCH_ARMV6:
+            # ARMV6 -- four byte align.
+            self.fun_data_attributes += ' __attribute__ ((aligned (4)))'
+        elif MP_NATIVE_ARCH_ARMV6M <= config.native_arch <= MP_NATIVE_ARCH_ARMV7EMDP:
+            # ARMVxxM -- two byte align.
+            self.fun_data_attributes += ' __attribute__ ((aligned (2)))'
+
     def _asm_thumb_rewrite_mov(self, pc, val):
         print('    (%u & 0xf0) | (%s >> 12),' % (self.bytecode[pc], val), end='')
         print(' (%u & 0xfb) | (%s >> 9 & 0x04),' % (self.bytecode[pc + 1], val), end='')
-- 
GitLab