Skip to content

Commit 145be62

Browse files
bitcoin3usclaude
andcommitted
Add build-time patches: absolute USER_C_MODULES, RISC-V frozen native alignment
Two build fixes as .patch files applied by MicroPythonOS's scripts/build_mpos.sh (see CONTRIBUTING.md), rather than edits to builder/ in this branch. esp32_user_c_modules_abspath.patch (apply in this repo's root): builder/esp32.py passed USER_C_MODULES=../../../../../ext_mod/micropython.cmake, which CMake resolves against lib/micropython/ports/esp32's real location. When lib/ is symlinked to another checkout (git worktrees sharing one MicroPython tree and build directory) that silently compiles the OTHER checkout's ext_mod. The patch passes the absolute path instead. esp32_riscv_frozen_native_align.patch (apply in lib/micropython): frozen @micropython.native / viper functions crashed with an illegal instruction on their first call on the ESP32-P4. tools/mpy-tool.py emits their machine code in a code section with aligned(2) for RISC-V, but the RISC-V assembler never pads a 2-byte alignment inside a code section, so every array following an odd-sized one starts at an odd address; jalr clears the low bit and the CPU decodes garbage. A 4-byte alignment is padded, but only when the file is assembled without linker relaxation (with relaxation the padding is R_RISCV_ALIGN NOPs, which fail on odd sizes: "3 bytes required for alignment ... can't relax section"). So the patch makes mpy-tool.py use aligned(4) for RV32/RV64, as it already does for Xtensa, and compiles frozen_content.c with -mno-relax on RISC-V targets. Xtensa builds are unchanged. Both were previously done by builder/esp32.py code in this PR. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 18d6f14 commit 145be62

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
diff --git a/ports/esp32/esp32_common.cmake b/ports/esp32/esp32_common.cmake
2+
index e3b1b81..6ec1f26 100644
3+
--- a/ports/esp32/esp32_common.cmake
4+
+++ b/ports/esp32/esp32_common.cmake
5+
@@ -311,6 +311,15 @@ endforeach()
6+
# Include the main MicroPython cmake rules.
7+
include(${MICROPY_DIR}/py/mkrules.cmake)
8+
9+
+if(CONFIG_IDF_TARGET_ARCH_RISCV AND MICROPY_FROZEN_CONTENT)
10+
+ # Frozen native code is emitted as byte arrays in a code section with a
11+
+ # 4-byte alignment (tools/mpy-tool.py). With linker relaxation on, the
12+
+ # assembler pads that alignment with R_RISCV_ALIGN NOPs, which cannot
13+
+ # cover odd sizes ("can't relax section"); the file holds no code that
14+
+ # could be relaxed, so assemble it without relaxation.
15+
+ set_source_files_properties(${MICROPY_FROZEN_CONTENT} PROPERTIES COMPILE_OPTIONS "-mno-relax")
16+
+endif()
17+
+
18+
# Generate source files for named pins (requires mkrules.cmake for MICROPY_GENHDR_DIR).
19+
20+
set(GEN_PINS_PREFIX "${MICROPY_PORT_DIR}/boards/pins_prefix.c")
21+
diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py
22+
index 67e2cbf..a61b0cc 100644
23+
--- a/tools/mpy-tool.py
24+
+++ b/tools/mpy-tool.py
25+
@@ -1254,13 +1254,19 @@ class RawCodeNative(RawCode):
26+
MP_NATIVE_ARCH_ARMV6,
27+
MP_NATIVE_ARCH_XTENSA,
28+
MP_NATIVE_ARCH_XTENSAWIN,
29+
+ MP_NATIVE_ARCH_RV32IMC,
30+
+ MP_NATIVE_ARCH_RV64IMC,
31+
):
32+
- # ARMV6 or Xtensa -- four byte align.
33+
+ # ARMV6, Xtensa or RISC-V -- four byte align. RISC-V needs only two,
34+
+ # but the assembler never pads a 2-byte alignment inside a code
35+
+ # section, so arrays after an odd-sized one would start at an odd
36+
+ # address (a 4-byte one is padded once relaxation is off, see
37+
+ # ports/esp32/esp32_common.cmake).
38+
self.fun_data_attributes += " __attribute__ ((aligned (4)))"
39+
elif (
40+
MP_NATIVE_ARCH_ARMV6M <= config.native_arch <= MP_NATIVE_ARCH_ARMV7EMDP
41+
- ) or MP_NATIVE_ARCH_RV32IMC <= config.native_arch <= MP_NATIVE_ARCH_RV64IMC:
42+
- # ARMVxxM or RV{32,64}IMC -- two byte align.
43+
+ ):
44+
+ # ARMVxxM -- two byte align.
45+
self.fun_data_attributes += " __attribute__ ((aligned (2)))"
46+
47+
def disassemble(self):

‎esp32_user_c_modules_abspath.patch‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
diff --git a/builder/esp32.py b/builder/esp32.py
2+
index 3f5fd15..bfc442d 100644
3+
--- a/builder/esp32.py
4+
+++ b/builder/esp32.py
5+
@@ -612,7 +612,9 @@ def build_commands(_, extra_args, script_dir, lv_cflags, ___):
6+
f'LV_CFLAGS="{lv_cflags}"',
7+
f'LV_PORT=esp32',
8+
f'BOARD={board}',
9+
- 'USER_C_MODULES=../../../../../ext_mod/micropython.cmake'
10+
+ # absolute: the relative form resolves against lib/micropython's real
11+
+ # location, which is a different tree when lib/ is symlinked (git worktrees)
12+
+ f'USER_C_MODULES={SCRIPT_DIR}/ext_mod/micropython.cmake'
13+
])
14+
15+
# esp_cmd.extend(extra_args)

0 commit comments

Comments
 (0)