From 58120f292c7b4ee3055b90a84b1083176b8c27ac Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 26 Jan 2026 23:57:15 +0100 Subject: [PATCH 01/22] partial arm thumb implementation added --- .gitignore | 1 + src/copapy/_binwrite.py | 4 +- src/copapy/_compiler.py | 9 +- src/copapy/_stencils.py | 26 +++++- src/coparun/runmem.c | 45 +++++++++ src/coparun/runmem.h | 1 + tests/test_ops_armv7thunb.py | 171 +++++++++++++++++++++++++++++++++++ tools/build.sh | 37 +++++++- tools/inspect.sh | 2 + 9 files changed, 287 insertions(+), 9 deletions(-) create mode 100644 tests/test_ops_armv7thunb.py diff --git a/.gitignore b/.gitignore index aaa1e38..dfe5d47 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ core *.log docs/source/start.md /src/copapy/_version.py +sketch*.py diff --git a/src/copapy/_binwrite.py b/src/copapy/_binwrite.py index ff77918..a12d82b 100644 --- a/src/copapy/_binwrite.py +++ b/src/copapy/_binwrite.py @@ -6,7 +6,9 @@ ByteOrder = Literal['little', 'big'] Command = Enum('Command', [('ALLOCATE_DATA', 1), ('COPY_DATA', 2), ('ALLOCATE_CODE', 3), ('COPY_CODE', 4), - ('PATCH_FUNC', 0x1000), ('PATCH_OBJECT', 0x2000), + ('PATCH_FUNC', 0x1000), + ('PATCH_FUNC_ARM32_THM', 0x1005), + ('PATCH_OBJECT', 0x2000), ('PATCH_OBJECT_HI21', 0x2001), ('PATCH_OBJECT_ABS', 0x2002), ('PATCH_OBJECT_REL', 0x2003), diff --git a/src/copapy/_compiler.py b/src/copapy/_compiler.py index cbade28..cd42d41 100644 --- a/src/copapy/_compiler.py +++ b/src/copapy/_compiler.py @@ -393,13 +393,14 @@ def compile_to_dag(node_list: Iterable[Node], sdb: stencil_database) -> tuple[bi # assemble stencils to main program and patch stencils data = sdb.get_function_code('entry_function_shell', 'start') data_list.append(data) + print(f"* entry_function_shell (0) " + ' '.join(f'{d:02X}' for d in data)) offset = aux_func_len + len(data) for associated_net, node in extended_output_ops: assert node.name in sdb.stencil_definitions, f"- Warning: {node.name} stencil not found" data = sdb.get_stencil_code(node.name) data_list.append(data) - #print(f"* {node.name} ({offset}) " + ' '.join(f'{d:02X}' for d in data)) + print(f"* {node.name} ({offset}) " + ' '.join(f'{d:02X}' for d in data)) for reloc in sdb.get_relocations(node.name, stencil=True): if reloc.target_symbol_info in ('STT_OBJECT', 'STT_NOTYPE', 'STT_SECTION'): @@ -451,10 +452,8 @@ def compile_to_dag(node_list: Iterable[Node], sdb: stencil_database) -> tuple[bi #print('--> ', name, list(sdb.get_relocations(name))) for reloc in sdb.get_relocations(name): - #assert reloc.target_symbol_info != 'STT_FUNC', "Not tested yet!" - if not reloc.target_section_index: - assert reloc.pelfy_reloc.type == 'R_ARM_V4BX' + assert reloc.pelfy_reloc.type == 'R_ARM_V4BX', (reloc.pelfy_reloc.type, name) elif reloc.target_symbol_info in {'STT_OBJECT', 'STT_NOTYPE', 'STT_SECTION'}: # Patch constants/variable addresses on heap @@ -489,6 +488,6 @@ def compile_to_dag(node_list: Iterable[Node], sdb: stencil_database) -> tuple[bi dw.write_int(patch.value, signed=True) dw.write_com(binw.Command.ENTRY_POINT) - dw.write_int(aux_func_len) + dw.write_int(aux_func_len + sdb.thumb_mode) return dw, variables diff --git a/src/copapy/_stencils.py b/src/copapy/_stencils.py index b7864c0..f464b3e 100644 --- a/src/copapy/_stencils.py +++ b/src/copapy/_stencils.py @@ -121,6 +121,7 @@ class stencil_database(): var_size (dict[str, int]): dictionary of object names and their sizes byteorder (ByteOrder): byte order of the ELF file elf (elf_file): the loaded ELF file + thumb_mode (int): 1 if ARM in thumb mode, 0 otherwise """ def __init__(self, obj_file: str | bytes): @@ -147,6 +148,11 @@ class stencil_database(): # if s.info == 'STT_OBJECT'} self.byteorder: ByteOrder = self.elf.byteorder + self.arm = '.ARM.attributes' in self.elf.sections + + # Returns 1 for ARM in thumb mode, 0 otherwise + self.thumb_mode = self.elf.symbols['entry_function_shell'].fields['st_value'] & 1 + #for name in self.function_definitions.keys(): # sym = self.elf.symbols[name] # sym.relocations @@ -314,6 +320,13 @@ class stencil_database(): patch_value = symbol_address + pr.fields['r_addend'] symbol_type = symbol_type + 0x03 # Relative to data section + elif pr.type.endswith('_THM_JUMP24') or pr.type.endswith('_THM_CALL'): + # R_ARM_THM_JUMP24 + # S + A - P + #assert pr.fields['r_addend'] == 0, pr.fields['r_addend'] + patch_value = symbol_address - (patch_offset + 4) #+ pr.fields['r_addend'] + symbol_type = symbol_type + 0x05 # PATCH_FUNC_ARM32_THM + else: raise NotImplementedError(f"Relocation type {pr.type} in {relocation.pelfy_reloc.target_section.name} pointing to {relocation.pelfy_reloc.symbol.name} not implemented") @@ -334,7 +347,11 @@ class stencil_database(): func = self.elf.symbols[name] start_stencil, end_stencil = get_stencil_position(func) assert func.section - start_index = func.section['sh_offset'] + func['st_value'] + start_stencil + + # For arm functions, mask out the thumb mode bit + function_offset = func.fields['st_value'] & ~int(self.arm) + + start_index = func.section['sh_offset'] + function_offset + start_stencil lengths = end_stencil - start_stencil self._stencil_cache[name] = (start_index, lengths) @@ -403,6 +420,13 @@ class stencil_database(): func = self.elf.symbols[name] assert func.info == 'STT_FUNC', f"{name} is not a function" + # For arm functions, mask out the thumb mode bit + function_offset = func.fields['st_value'] & ~int(self.arm) + + start_index = func.section['sh_offset'] + function_offset + lengths = end_stencil - start_stencil + + #return self.elf.read_bytes(start_index, func.fields['st_size']) if part == 'start': index = get_last_call_in_function(func) return func.data[:index] diff --git a/src/coparun/runmem.c b/src/coparun/runmem.c index 1165956..e5180e5 100644 --- a/src/coparun/runmem.c +++ b/src/coparun/runmem.c @@ -50,6 +50,41 @@ void patch_arm32_abs(uint8_t *patch_addr, uint32_t imm16) *((uint32_t *)patch_addr) = instr; } +void patch_arm_thm_jump24(uint8_t *patch_addr, int32_t imm24) +{ + // Read the 32-bit instruction (two halfwords) + uint16_t *instr16 = (uint16_t *)patch_addr; + uint16_t first_half = instr16[0]; + uint16_t second_half = instr16[1]; + + // Thumb branch instructions always have LSB = 0 (halfword aligned) + // The imm24 offset in Thumb is shifted right by 1 when encoded + int32_t offset = imm24 >> 1; + + // Split into S, J1, J2, imm10, imm11 + uint32_t S = (offset >> 23) & 0x1; + uint32_t I1 = (offset >> 22) & 0x1; + uint32_t I2 = (offset >> 21) & 0x1; + uint32_t imm10 = (offset >> 11) & 0x3FF; + uint32_t imm11 = offset & 0x7FF; + + // Re-encode J1 and J2 + uint32_t J1 = (~(I1 ^ S)) & 0x1; + uint32_t J2 = (~(I2 ^ S)) & 0x1; + + // Clear old imm fields + first_half &= 0xF800; // Keep upper 5 bits + second_half &= 0xD000; // Keep upper 5 bits + + // Set new imm fields + first_half |= (S << 10) | imm10; + second_half |= (J1 << 13) | (J2 << 11) | imm11; + + // Write back + instr16[0] = first_half; + instr16[1] = second_half; +} + void free_memory(runmem_t *context) { deallocate_memory(context->executable_memory, context->executable_memory_len); deallocate_memory(context->data_memory, context->data_memory_len); @@ -180,6 +215,16 @@ int parse_commands(runmem_t *context, uint8_t *bytes) { patch_arm32_abs(context->executable_memory + offs, (uint32_t)((uintptr_t)(context->data_memory + value) & patch_mask) / (uint32_t)patch_scale); break; + case PATCH_FUNC_ARM32_THM: + offs = *(uint32_t*)bytes; bytes += 4; + patch_mask = *(uint32_t*)bytes; bytes += 4; + patch_scale = *(int32_t*)bytes; bytes += 4; + value = *(int32_t*)bytes; bytes += 4; + LOG("PATCH_FUNC_ARM32_THM patch_offs=%i patch_mask=%#08x scale=%i value=%i\n", + offs, patch_mask, patch_scale, value); + patch_arm_thm_jump24(context->executable_memory + offs, value); + break; + case ENTRY_POINT: rel_entr_point = *(uint32_t*)bytes; bytes += 4; context->entr_point = (entry_point_t)(context->executable_memory + rel_entr_point); diff --git a/src/coparun/runmem.h b/src/coparun/runmem.h index 31e9905..371f2f3 100644 --- a/src/coparun/runmem.h +++ b/src/coparun/runmem.h @@ -20,6 +20,7 @@ #define ALLOCATE_CODE 3 #define COPY_CODE 4 #define PATCH_FUNC 0x1000 +#define PATCH_FUNC_ARM32_THM 0x1005 #define PATCH_OBJECT 0x2000 #define PATCH_OBJECT_HI21 0x2001 #define PATCH_OBJECT_ABS 0x2002 diff --git a/tests/test_ops_armv7thunb.py b/tests/test_ops_armv7thunb.py new file mode 100644 index 0000000..ac67e51 --- /dev/null +++ b/tests/test_ops_armv7thunb.py @@ -0,0 +1,171 @@ +from copapy import NumLike, iif, value +from copapy.backend import Store, compile_to_dag, add_read_value_remote +import subprocess +from copapy import _binwrite +import copapy.backend as backend +import os +import warnings +import re +import struct +import pytest +import copapy as cp + +if os.name == 'nt': + # On Windows wsl and qemu-user is required: + # sudo apt install qemu-user + qemu_command = ['wsl', 'qemu-arm'] +else: + qemu_command = ['qemu-arm'] + + +def parse_results(log_text: str) -> dict[int, bytes]: + regex = r"^READ_DATA offs=(\d*) size=(\d*) data=(.*)$" + matches = re.finditer(regex, log_text, re.MULTILINE) + var_dict: dict[int, bytes] = {} + + for match in matches: + value_str: list[str] = match.group(3).strip().split(' ') + #print('--', value_str) + value = bytes(int(v, base=16) for v in value_str) + if len(value) <= 8: + var_dict[int(match.group(1))] = value + + return var_dict + + +def run_command(command: list[str]) -> str: + result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf8', check=False) + assert result.returncode != 11, f"SIGSEGV (segmentation fault)\n -Error occurred: {result.stderr}\n -Output: {result.stdout}" + assert result.returncode == 0, f"\n -Error occurred: {result.stderr}\n -Output: {result.stdout}" + return result.stdout + + +def check_for_qemu() -> bool: + command = qemu_command + ['--version'] + try: + result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) + except Exception: + return False + return result.returncode == 0 + + +def function1(c1: NumLike) -> list[NumLike]: + return [c1 / 4, c1 / -4, c1 // 4, c1 // -4, (c1 * -1) // 4, + c1 * 4, c1 * -4, + c1 + 4, c1 - 4, + c1 > 2, c1 > 100, c1 < 4, c1 < 100] + + +def function2(c1: NumLike) -> list[NumLike]: + return [c1 * 4.44, c1 * -4.44] + + +def function3(c1: NumLike) -> list[NumLike]: + return [c1 / 4] + + +def function4(c1: NumLike) -> list[NumLike]: + return [c1 == 9, c1 == 4, c1 != 9, c1 != 4] + + +def function5(c1: NumLike) -> list[NumLike]: + return [c1 == True, c1 == False, c1 != True, c1 != False, c1 / 2, c1 + 2] + + +def function6(c1: NumLike) -> list[NumLike]: + return [c1 == True] + + +def iiftests(c1: NumLike) -> list[NumLike]: + return [iif(c1 > 5, 8, 9), + iif(c1 < 5, 8.5, 9.5), + iif(1 > 5, 3.3, 8.8) + c1, + iif(1 < 5, c1 * 3.3, 8.8), + iif(c1 < 5, c1 * 3.3, 8.8)] + + +@pytest.mark.runner +def test_compile(): + c_i = value(9) + c_f = value(1.111) + c_b = value(True) + + #ret_test = function1(c_i) + function1(c_f) + function2(c_i) + function2(c_f) + function3(c_i) + function4(c_i) + function5(c_b) + [value(9) % 2] + iiftests(c_i) + iiftests(c_f) + [cp.asin(c_i/10)] + #ret_ref = function1(9) + function1(1.111) + function2(9) + function2(1.111) + function3(9) + function4(9) + function5(True) + [9 % 2] + iiftests(9) + iiftests(1.111) + [cp.asin(9/10)] + + ret_test = (c_i * 100 + 5,) + ret_ref = (9 * 100 + 5,) + + out = [Store(r) for r in ret_test] + + sdb = backend.stencil_db_from_package('armv7thumb') + dw, variables = compile_to_dag(out, sdb) + + #dw.write_com(_binwrite.Command.READ_DATA) + #dw.write_int(0) + #dw.write_int(28) + + # run program command + #dw.write_com(_binwrite.Command.RUN_PROG) + dw.write_com(_binwrite.Command.DUMP_CODE) + + for v in ret_test: + assert isinstance(v, value) + add_read_value_remote(dw, variables, v.net) + + #dw.write_com(_binwrite.Command.READ_DATA) + #dw.write_int(0) + #dw.write_int(28) + + dw.write_com(_binwrite.Command.END_COM) + + #print('* Data to runner:') + #dw.print() + + dw.to_file('build/runner/test-armv7thumb.copapy') + + if not check_for_qemu(): + warnings.warn("qemu-armv7 not found, armv7 test skipped!", UserWarning) + return + if not os.path.isfile('build/runner/coparun-armv7thumb'): + warnings.warn("armv7thumb runner not found, armv7thumb test skipped!", UserWarning) + return + + command = qemu_command + ['build/runner/coparun-armv7thumb', 'build/runner/test-armv7thumb.copapy'] + ['build/runner/test.copapy-armv7thumb.bin'] + #try: + result = run_command(command) + #except FileNotFoundError: + # warnings.warn(f"Test skipped, executable not found.", UserWarning) + # return + + print('* Output from runner:\n--') + print(result) + print('--') + + assert 'Return value: 1' in result + + result_data = parse_results(result) + + for test, ref in zip(ret_test, ret_ref): + assert isinstance(test, value) + address = variables[test.net][0] + data = result_data[address] + if test.dtype == 'int': + val = int.from_bytes(data, sdb.byteorder, signed=True) + elif test.dtype == 'bool': + val = bool.from_bytes(data, sdb.byteorder) + elif test.dtype == 'float': + en = {'little': '<', 'big': '>'}[sdb.byteorder] + val = struct.unpack(en + 'f', data)[0] + assert isinstance(val, float) + else: + raise Exception(f"Unknown type: {test.dtype}") + print('+', val, ref, test.dtype, f" addr={address}") + for t in (int, float, bool): + assert isinstance(val, t) == isinstance(ref, t), f"Result type does not match for {val} and {ref}" + assert val == pytest.approx(ref, 1e-5), f"Result does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType] + + +if __name__ == "__main__": + #test_example() + test_slow_31bit_int_list_hash() diff --git a/tools/build.sh b/tools/build.sh index b7e3cd6..fca6f7e 100644 --- a/tools/build.sh +++ b/tools/build.sh @@ -4,10 +4,10 @@ set -eux ARCH=${1:-x86_64} case "$ARCH" in - (x86_64|arm-v6|arm-v7|all) + (x86_64|arm-v6|arm-v7|arm-v7-thumb|all) ;; (*) - echo "Usage: $0 [x86_64|arm-v6|arm-v7|all]" + echo "Usage: $0 [x86_64|arm-v6|arm-v7|arm-v6-thumb|all]" exit 1 ;; esac @@ -107,3 +107,36 @@ if [[ "$ARCH" == "arm-v7" || "$ARCH" == "all" ]]; then src/coparun/mem_man.c \ -o build/runner/coparun-armv7 fi + +####################################### +# ARM v7 thumb +####################################### +if [[ "$ARCH" == "arm-v7-thumb" || "$ARCH" == "all" ]]; then + echo "--------------arm-v7-thumb 32 bit----------------" + + LIBGCC=$(arm-none-eabi-gcc -print-libgcc-file-name) + + arm-none-eabi-gcc -fno-pic -ffunction-sections \ + -march=armv7e-m -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb \ + -c $SRC -O3 -o build/stencils/stencils.o + + arm-none-eabi-ld -r \ + build/stencils/stencils.o \ + build/musl/musl_objects_armv7thumb.o \ + $LIBGCC \ + -o $DEST/stencils_armv7thumb_O3.o + + arm-none-eabi-objdump -d -x \ + $DEST/stencils_armv7thumb_O3.o \ + > build/stencils/stencils_armv7thumb_O3.asm + + arm-linux-gnueabihf-gcc \ + -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -mthumb -static \ + -Wall -Wextra -Wconversion -Wsign-conversion \ + -Wshadow -Wstrict-overflow -O3 \ + -DENABLE_LOGGING \ + src/coparun/runmem.c \ + src/coparun/coparun.c \ + src/coparun/mem_man.c \ + -o build/runner/coparun-armv7thumb +fi \ No newline at end of file diff --git a/tools/inspect.sh b/tools/inspect.sh index c25e898..42fba43 100644 --- a/tools/inspect.sh +++ b/tools/inspect.sh @@ -14,3 +14,5 @@ objdump -D -b binary -m i386:x86-64 --adjust-vma=0x1000 build/runner/test.copapy build/runner/coparun-armv7 build/runner/test-armv7.copapy build/runner/test.copapy-armv7.bin arm-none-eabi-objdump -D -b binary -marm --adjust-vma=0x50000 build/runner/test.copapy-armv7.bin > build/runner/test.copapy-armv7.asm + +# arm-none-eabi-objdump -D -b binary -marm -M force-thumb --adjust-vma=0x50001 build/runner/test.copapy-armv7thumb.bin > build/runner/test.copapy-armv7thumb.asm From bc0ccd90b746e7cf49437fead765fa272d6011c8 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Thu, 12 Feb 2026 23:54:20 +0100 Subject: [PATCH 02/22] Updated pelfy and using offset_in_section instead of fields['st_value'] --- src/copapy/_stencils.py | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/src/copapy/_stencils.py b/src/copapy/_stencils.py index f464b3e..1c3c9ef 100644 --- a/src/copapy/_stencils.py +++ b/src/copapy/_stencils.py @@ -102,7 +102,7 @@ def get_last_call_in_function(func: pelfy.elf_symbol) -> int: instruction_lengths = 4 if reloc.bits < 32 else 5 address_field_length = 4 #print(f"-> {[r.fields['r_offset'] - func.fields['st_value'] for r in func.relocations]}") - return reloc.fields['r_offset'] - func.fields['st_value'] + address_field_length - instruction_lengths + return reloc.fields['r_offset'] - func.offset_in_section + address_field_length - instruction_lengths def get_op_after_last_call_in_function(func: pelfy.elf_symbol) -> int: @@ -110,7 +110,7 @@ def get_op_after_last_call_in_function(func: pelfy.elf_symbol) -> int: assert func.relocations, f'No call function in stencil function {func.name}.' reloc = func.relocations[-1] assert reloc.bits <= 32, "Relocation segment might be larger then 32 bit" - return reloc.fields['r_offset'] - func.fields['st_value'] + 4 + return reloc.fields['r_offset'] - func.offset_in_section + 4 class stencil_database(): @@ -121,7 +121,7 @@ class stencil_database(): var_size (dict[str, int]): dictionary of object names and their sizes byteorder (ByteOrder): byte order of the ELF file elf (elf_file): the loaded ELF file - thumb_mode (int): 1 if ARM in thumb mode, 0 otherwise + thumb_mode (bool): entry_function_shell in ARM thumb mode """ def __init__(self, obj_file: str | bytes): @@ -148,10 +148,7 @@ class stencil_database(): # if s.info == 'STT_OBJECT'} self.byteorder: ByteOrder = self.elf.byteorder - self.arm = '.ARM.attributes' in self.elf.sections - - # Returns 1 for ARM in thumb mode, 0 otherwise - self.thumb_mode = self.elf.symbols['entry_function_shell'].fields['st_value'] & 1 + self.thumb_mode = self.elf.symbols['entry_function_shell'].thumb_mode #for name in self.function_definitions.keys(): # sym = self.elf.symbols[name] @@ -194,14 +191,14 @@ class stencil_database(): for reloc in symbol.relocations: # address to fist byte to patch relative to the start of the symbol - patch_offset = reloc.fields['r_offset'] - symbol.fields['st_value'] - start_index + patch_offset = reloc.fields['r_offset'] - symbol.offset_in_section - start_index if patch_offset < end_index - start_index: # Exclude the call to the result_* function reloc_entry = relocation_entry(reloc.symbol.name, reloc.symbol.info, - reloc.symbol.fields['st_value'], + reloc.symbol.fields['st_value'], # LSB on ARM indicates thumb mode reloc.symbol.fields['st_shndx'], - symbol.fields['st_value'], + symbol.offset_in_section, start_index, reloc) cache.append(reloc_entry) @@ -348,10 +345,7 @@ class stencil_database(): start_stencil, end_stencil = get_stencil_position(func) assert func.section - # For arm functions, mask out the thumb mode bit - function_offset = func.fields['st_value'] & ~int(self.arm) - - start_index = func.section['sh_offset'] + function_offset + start_stencil + start_index = func.offset_in_file + start_stencil lengths = end_stencil - start_stencil self._stencil_cache[name] = (start_index, lengths) @@ -389,7 +383,7 @@ class stencil_database(): def get_symbol_offset(self, name: str) -> int: """Returns the offset of a specified symbol in the section.""" - return self.elf.symbols[name].fields['st_value'] + return self.elf.symbols[name].offset_in_section def get_symbol_section_index(self, name: str) -> int: """Returns the section index for a specified symbol name.""" @@ -420,13 +414,6 @@ class stencil_database(): func = self.elf.symbols[name] assert func.info == 'STT_FUNC', f"{name} is not a function" - # For arm functions, mask out the thumb mode bit - function_offset = func.fields['st_value'] & ~int(self.arm) - - start_index = func.section['sh_offset'] + function_offset - lengths = end_stencil - start_stencil - - #return self.elf.read_bytes(start_index, func.fields['st_size']) if part == 'start': index = get_last_call_in_function(func) return func.data[:index] From a81236a3fca47eafcc94e7ac6b8c93249a42ecf1 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Fri, 13 Feb 2026 00:51:55 +0100 Subject: [PATCH 03/22] 4-Byte-Alignment error on ARM thumb fixed by using section size instead of function size to include nop padding --- src/copapy/_stencils.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/copapy/_stencils.py b/src/copapy/_stencils.py index 1c3c9ef..eb81aba 100644 --- a/src/copapy/_stencils.py +++ b/src/copapy/_stencils.py @@ -80,11 +80,20 @@ def get_return_function_type(symbol: pelfy.elf_symbol) -> str: def get_stencil_position(func: pelfy.elf_symbol) -> tuple[int, int]: start_index = 0 # There must be no prolog + # Find last relocation in function last_instr = get_last_call_in_function(func) - function_size = func.fields['st_size'] - if last_instr + 5 >= function_size: # Check if jump is last instruction - end_index = last_instr # Jump can be striped + + assert func.section, f"No code section specified for symbol {func.name}" + + # func.section.fields['sh_size'] is equivalent to func.fields['st_size'] + # expect for ARM thumb, here nop padding at the end for 4-byte alignment + # is not included in st_size + function_size = func.section.fields['sh_size'] + + # Check if jump is the last instruction and can be striped + if last_instr + 5 >= function_size: + end_index = last_instr else: end_index = function_size @@ -98,7 +107,8 @@ def get_last_call_in_function(func: pelfy.elf_symbol) -> int: if reloc.symbol.name.startswith('dummy_'): return -0xFFFF # Last relocation is not a jump else: - # Assume the call instruction is 4 bytes long for relocations with less than 32 bit and 5 bytes otherwise + # Assume the jump/call instruction is 4 bytes long for relocations + # with less than 32 bit and 5 bytes otherwise instruction_lengths = 4 if reloc.bits < 32 else 5 address_field_length = 4 #print(f"-> {[r.fields['r_offset'] - func.fields['st_value'] for r in func.relocations]}") From 83ce6ce0e7f6bdf518f999351e3c877be0f55e84 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Fri, 13 Feb 2026 01:13:01 +0100 Subject: [PATCH 04/22] musl functions for math on ARM thumb added to stencil build pipeline --- tools/cross_compiler_unix/packobjs.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/cross_compiler_unix/packobjs.sh b/tools/cross_compiler_unix/packobjs.sh index 43bd0e9..ff5f32a 100644 --- a/tools/cross_compiler_unix/packobjs.sh +++ b/tools/cross_compiler_unix/packobjs.sh @@ -25,14 +25,16 @@ ar x ../../musl/lib/libc.a sinf.o cosf.o tanf.o asinf.o acosf.o atanf.o atan2f.o ar x ../../musl/lib/libc.a sqrtf.o logf.o expf.o sqrt.o ar x ../../musl/lib/libc.a logf_data.o __tandf.o __cosdf.o __sindf.o ar x ../../musl/lib/libc.a fabsf.o scalbn.o floor.o floorf.o exp2f_data.o powf.o powf_data.o -ar x ../../musl/lib/libc.a __rem_pio2f.o __math_invalidf.o __stack_chk_fail.o __math_divzerof.o __math_oflowf.o __rem_pio2_large.o __math_uflowf.o __math_xflowf.o +ar x ../../musl/lib/libc.a __rem_pio2f.o __math_invalid.o __math_invalidf.o __stack_chk_fail.o +ar x ../../musl/lib/libc.a __math_divzerof.o __math_oflowf.o __rem_pio2_large.o __math_uflowf.o __math_xflowf.o __rsqrt_tab.o # Check out .lo (PIC) ar x ../../musl/lib/libc.a sinf.lo cosf.lo tanf.lo asinf.lo acosf.lo atanf.lo atan2f.lo ar x ../../musl/lib/libc.a sqrtf.lo logf.lo expf.lo sqrt.lo ar x ../../musl/lib/libc.a logf_data.lo __tandf.lo __cosdf.lo __sindf.lo ar x ../../musl/lib/libc.a fabsf.lo scalbn.lo floor.lo floorf.o exp2f_data.lo powf.lo powf_data.lo -ar x ../../musl/lib/libc.a __rem_pio2f.lo __math_invalidf.lo __stack_chk_fail.lo __math_divzerof.lo __math_oflowf.lo __rem_pio2_large.lo __math_uflowf.lo __math_xflowf.lo +ar x ../../musl/lib/libc.a __rem_pio2f.lo __math_invalid.lo __math_invalidf.lo __stack_chk_fail.lo +ar x ../../musl/lib/libc.a __math_divzerof.lo __math_oflowf.lo __rem_pio2_large.lo __math_uflowf.lo __math_xflowf.lo __rsqrt_tab.lo cd ../../musl From cabfda4ec6977a71e3a99b4dc613381a88c6ae0a Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sat, 28 Feb 2026 22:07:27 +0100 Subject: [PATCH 05/22] CI: stencil build script updated with Cortex-A Thumb version --- tools/cross_compiler_unix/build_musl.sh | 5 ++++- tools/cross_compiler_unix/packobjs.sh | 4 ++-- tools/crosscompile.sh | 9 +++++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/tools/cross_compiler_unix/build_musl.sh b/tools/cross_compiler_unix/build_musl.sh index 2f16b1e..3f7b50e 100644 --- a/tools/cross_compiler_unix/build_musl.sh +++ b/tools/cross_compiler_unix/build_musl.sh @@ -26,8 +26,11 @@ sh ../packobjs.sh arm-none-eabi-gcc arm-none-eabi-ld /object_files/musl_objects_ # Armv7 sh ../packobjs.sh arm-none-eabi-gcc arm-none-eabi-ld /object_files/musl_objects_armv7.o "-march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -marm" +# Armv7 Thumb for Cortex-A +sh ../packobjs.sh arm-none-eabi-gcc arm-none-eabi-ld /object_files/musl_objects_armv7thumb.o "-march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -mthumb" + # Armv7 Thumb for Cortex-M3..7 -sh ../packobjs.sh arm-none-eabi-gcc arm-none-eabi-ld /object_files/musl_objects_armv7thumb.o "-march=armv7e-m -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb" +sh ../packobjs.sh arm-none-eabi-gcc arm-none-eabi-ld /object_files/musl_objects_armv7mthumb.o "-march=armv7e-m -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb" #sh ../packobjs.sh mips mips-linux-gnu-gcc-13 mips-linux-gnu-ld diff --git a/tools/cross_compiler_unix/packobjs.sh b/tools/cross_compiler_unix/packobjs.sh index ff5f32a..98e3c17 100644 --- a/tools/cross_compiler_unix/packobjs.sh +++ b/tools/cross_compiler_unix/packobjs.sh @@ -26,7 +26,7 @@ ar x ../../musl/lib/libc.a sqrtf.o logf.o expf.o sqrt.o ar x ../../musl/lib/libc.a logf_data.o __tandf.o __cosdf.o __sindf.o ar x ../../musl/lib/libc.a fabsf.o scalbn.o floor.o floorf.o exp2f_data.o powf.o powf_data.o ar x ../../musl/lib/libc.a __rem_pio2f.o __math_invalid.o __math_invalidf.o __stack_chk_fail.o -ar x ../../musl/lib/libc.a __math_divzerof.o __math_oflowf.o __rem_pio2_large.o __math_uflowf.o __math_xflowf.o __rsqrt_tab.o +ar x ../../musl/lib/libc.a __math_divzerof.o __math_oflowf.o __rem_pio2_large.o __math_uflowf.o __math_xflowf.o sqrt_data.o # Check out .lo (PIC) ar x ../../musl/lib/libc.a sinf.lo cosf.lo tanf.lo asinf.lo acosf.lo atanf.lo atan2f.lo @@ -34,7 +34,7 @@ ar x ../../musl/lib/libc.a sqrtf.lo logf.lo expf.lo sqrt.lo ar x ../../musl/lib/libc.a logf_data.lo __tandf.lo __cosdf.lo __sindf.lo ar x ../../musl/lib/libc.a fabsf.lo scalbn.lo floor.lo floorf.o exp2f_data.lo powf.lo powf_data.lo ar x ../../musl/lib/libc.a __rem_pio2f.lo __math_invalid.lo __math_invalidf.lo __stack_chk_fail.lo -ar x ../../musl/lib/libc.a __math_divzerof.lo __math_oflowf.lo __rem_pio2_large.lo __math_uflowf.lo __math_xflowf.lo __rsqrt_tab.lo +ar x ../../musl/lib/libc.a __math_divzerof.lo __math_oflowf.lo __rem_pio2_large.lo __math_uflowf.lo __math_xflowf.lo sqrt_data.lo cd ../../musl diff --git a/tools/crosscompile.sh b/tools/crosscompile.sh index 9494ff2..29a15ca 100644 --- a/tools/crosscompile.sh +++ b/tools/crosscompile.sh @@ -36,15 +36,20 @@ arm-none-eabi-gcc -march=armv6 -mfpu=vfp -mfloat-abi=hard -marm $FLAGS -$OPT -c LIBGCC=$(arm-none-eabi-gcc -print-libgcc-file-name) arm-none-eabi-ld -r $STMP /object_files/musl_objects_armv6.o $LIBGCC -o $DEST/stencils_armv6_$OPT.o -# ARMv7 hardware fp +# ARMv7 hardware fp for Cortex-A arm-none-eabi-gcc -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -marm $FLAGS -$OPT -c $SRC -o $STMP LIBGCC=$(arm-none-eabi-gcc -print-libgcc-file-name) arm-none-eabi-ld -r $STMP /object_files/musl_objects_armv7.o $LIBGCC -o $DEST/stencils_armv7_$OPT.o +# ARMv7 Thumb for Cortex-A with hardware fp +arm-none-eabi-gcc -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -mthumb $FLAGS -$OPT -c $SRC -o $STMP +LIBGCC=$(arm-none-eabi-gcc -print-libgcc-file-name) +arm-none-eabi-ld -r $STMP /object_files/musl_objects_armv7thumb.o $LIBGCC -o $DEST/stencils_armv7thumb_$OPT.o + # Armv7 Thumb for Cortex-M3..7 hardware fp arm-none-eabi-gcc -march=armv7e-m -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb $FLAGS -$OPT -c $SRC -o $STMP LIBGCC=$(arm-none-eabi-gcc -print-libgcc-file-name) -arm-none-eabi-ld -r $STMP /object_files/musl_objects_armv7thumb.o $LIBGCC -o $DEST/stencils_armv7thumb_$OPT.o +arm-none-eabi-ld -r $STMP /object_files/musl_objects_armv7mthumb.o $LIBGCC -o $DEST/stencils_armv7mthumb_$OPT.o # PowerPC64LE # powerpc64le-linux-gnu-gcc-13 $FLAGS -$OPT -c $SRC -o $DEST/stencils_ppc64le_$OPT.o From 436a09c1eac61cbab068520c76066063cdc460e5 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sat, 28 Feb 2026 22:08:22 +0100 Subject: [PATCH 06/22] copy method to data_writer added --- src/copapy/_binwrite.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/copapy/_binwrite.py b/src/copapy/_binwrite.py index a12d82b..e1d7c90 100644 --- a/src/copapy/_binwrite.py +++ b/src/copapy/_binwrite.py @@ -24,6 +24,11 @@ class data_writer(): self._data: list[tuple[str, bytes, int]] = [] self.byteorder: ByteOrder = byteorder + def copy(self): + cp = data_writer(self.byteorder) + cp._data = self._data.copy() + return cp + def write_int(self, value: int, num_bytes: int = 4, signed: bool = False) -> None: self._data.append((f"INT {value}", value.to_bytes(length=num_bytes, byteorder=self.byteorder, signed=signed), 0)) From afc442ada62053d16e78b0cec6f4d34237907f45 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sat, 28 Feb 2026 22:09:12 +0100 Subject: [PATCH 07/22] stencil build script updated --- tools/build.sh | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/tools/build.sh b/tools/build.sh index fca6f7e..4af186a 100644 --- a/tools/build.sh +++ b/tools/build.sh @@ -1,13 +1,13 @@ #!/bin/bash -set -eux +set -eu ARCH=${1:-x86_64} case "$ARCH" in - (x86_64|arm-v6|arm-v7|arm-v7-thumb|all) + (x86_64|arm-v6|arm-v7|arm-v7-thumb|arm-v7m-thumb|all) ;; (*) - echo "Usage: $0 [x86_64|arm-v6|arm-v7|arm-v6-thumb|all]" + echo "Usage: $0 [x86_64|arm-v6|arm-v7|arm-v6-thumb|arm-v7m-thumb|all]" exit 1 ;; esac @@ -109,20 +109,20 @@ if [[ "$ARCH" == "arm-v7" || "$ARCH" == "all" ]]; then fi ####################################### -# ARM v7 thumb +# ARM v7 thumb Cortex-A ####################################### if [[ "$ARCH" == "arm-v7-thumb" || "$ARCH" == "all" ]]; then - echo "--------------arm-v7-thumb 32 bit----------------" + echo "--------------arm-v7a-thumb 32 bit----------------" LIBGCC=$(arm-none-eabi-gcc -print-libgcc-file-name) arm-none-eabi-gcc -fno-pic -ffunction-sections \ - -march=armv7e-m -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb \ + -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -mthumb \ -c $SRC -O3 -o build/stencils/stencils.o arm-none-eabi-ld -r \ build/stencils/stencils.o \ - build/musl/musl_objects_armv7thumb.o \ + build/musl/musl_objects_armv7.o \ $LIBGCC \ -o $DEST/stencils_armv7thumb_O3.o @@ -130,6 +130,39 @@ if [[ "$ARCH" == "arm-v7-thumb" || "$ARCH" == "all" ]]; then $DEST/stencils_armv7thumb_O3.o \ > build/stencils/stencils_armv7thumb_O3.asm + arm-linux-gnueabihf-gcc \ + -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -mthumb -static \ + -Wall -Wextra -Wconversion -Wsign-conversion \ + -Wshadow -Wstrict-overflow -O3 \ + -DENABLE_LOGGING \ + src/coparun/runmem.c \ + src/coparun/coparun.c \ + src/coparun/mem_man.c \ + -o build/runner/coparun-armv7thumb +fi + +####################################### +# ARM v7 thumb Cortex-M +####################################### +if [[ "$ARCH" == "arm-v7m-thumb" || "$ARCH" == "all" ]]; then + echo "--------------arm-v7m-thumb 32 bit----------------" + + LIBGCC=$(arm-none-eabi-gcc -print-libgcc-file-name) + + arm-none-eabi-gcc -fno-pic -ffunction-sections \ + -march=armv7e-m -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb \ + -c $SRC -O3 -o build/stencils/stencils.o + + arm-none-eabi-ld -r \ + build/stencils/stencils.o \ + build/musl/musl_objects_armv7mthumb.o \ + $LIBGCC \ + -o $DEST/stencils_armv7mthumb_O3.o + + arm-none-eabi-objdump -d -x \ + $DEST/stencils_armv7mthumb_O3.o \ + > build/stencils/stencils_armv7mthumb_O3.asm + arm-linux-gnueabihf-gcc \ -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -mthumb -static \ -Wall -Wextra -Wconversion -Wsign-conversion \ From 8fcf0dedac588777df63e1fcbbcb3cc3fd2814d1 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 2 Mar 2026 21:28:05 +0100 Subject: [PATCH 08/22] patch type added: PATCH_OBJECT_ARM32_ABS_THM (for R_ARM_THM_MOVW_ABS_NC and R_ARM_THM_MOVT_ABS) --- src/copapy/_binwrite.py | 1 + src/coparun/runmem.c | 40 ++++++++++++++++++++++++++++++++++++++-- src/coparun/runmem.h | 1 + 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/copapy/_binwrite.py b/src/copapy/_binwrite.py index e1d7c90..1af2ee3 100644 --- a/src/copapy/_binwrite.py +++ b/src/copapy/_binwrite.py @@ -13,6 +13,7 @@ Command = Enum('Command', [('ALLOCATE_DATA', 1), ('COPY_DATA', 2), ('PATCH_OBJECT_ABS', 0x2002), ('PATCH_OBJECT_REL', 0x2003), ('PATCH_OBJECT_ARM32_ABS', 0x2004), + ('PATCH_OBJECT_ARM32_ABS_THM', 0x2006), ('ENTRY_POINT', 7), ('RUN_PROG', 64), ('READ_DATA', 65), ('END_COM', 256), ('FREE_MEMORY', 257), ('DUMP_CODE', 258)]) diff --git a/src/coparun/runmem.c b/src/coparun/runmem.c index e5180e5..b905e97 100644 --- a/src/coparun/runmem.c +++ b/src/coparun/runmem.c @@ -50,6 +50,32 @@ void patch_arm32_abs(uint8_t *patch_addr, uint32_t imm16) *((uint32_t *)patch_addr) = instr; } +void patch_arm_thm_abs(uint8_t *patch_addr, uint32_t imm16) +{ + // Thumb MOVW (T3) / MOVT (T1) encoding + + uint16_t *instr16 = (uint16_t *)patch_addr; + uint16_t first_half = instr16[0]; + uint16_t second_half = instr16[1]; + + // Extract fields from imm16 + uint32_t imm4 = (imm16 >> 12) & 0xF; + uint32_t i = (imm16 >> 11) & 0x1; + uint32_t imm3 = (imm16 >> 8) & 0x7; + uint32_t imm8 = imm16 & 0xFF; + + // Clear bits + first_half &= (uint16_t)(~(0x000F | (1 << 10))); + second_half &= (uint16_t)(~(0x00FF | (0x7 << 12))); + + // Set new fields + first_half |= (uint16_t)((imm4 << 0) | (i << 10)); + second_half |= (uint16_t)(imm8 | (imm3 << 12)); + + instr16[0] = first_half; + instr16[1] = second_half; +} + void patch_arm_thm_jump24(uint8_t *patch_addr, int32_t imm24) { // Read the 32-bit instruction (two halfwords) @@ -77,8 +103,8 @@ void patch_arm_thm_jump24(uint8_t *patch_addr, int32_t imm24) second_half &= 0xD000; // Keep upper 5 bits // Set new imm fields - first_half |= (S << 10) | imm10; - second_half |= (J1 << 13) | (J2 << 11) | imm11; + first_half |= (uint16_t)((S << 10) | imm10); + second_half |= (uint16_t)((J1 << 13) | (J2 << 11) | imm11); // Write back instr16[0] = first_half; @@ -225,6 +251,16 @@ int parse_commands(runmem_t *context, uint8_t *bytes) { patch_arm_thm_jump24(context->executable_memory + offs, value); break; + case PATCH_OBJECT_ARM32_ABS_THM: + offs = *(uint32_t*)bytes; bytes += 4; + patch_mask = *(uint32_t*)bytes; bytes += 4; + patch_scale = *(int32_t*)bytes; bytes += 4; + value = *(int32_t*)bytes; bytes += 4; + LOG("PATCH_OBJECT_ARM32_ABS_THM patch_offs=%i patch_mask=%#08x scale=%i value=%i imm16=%#04x\n", + offs, patch_mask, patch_scale, value, (uint32_t)((uintptr_t)(context->data_memory + value) & patch_mask) / (uint32_t)patch_scale); + patch_arm_thm_abs(context->executable_memory + offs, (uint32_t)((uintptr_t)(context->data_memory + value) & patch_mask) / (uint32_t)patch_scale); + break; + case ENTRY_POINT: rel_entr_point = *(uint32_t*)bytes; bytes += 4; context->entr_point = (entry_point_t)(context->executable_memory + rel_entr_point); diff --git a/src/coparun/runmem.h b/src/coparun/runmem.h index 371f2f3..17da047 100644 --- a/src/coparun/runmem.h +++ b/src/coparun/runmem.h @@ -26,6 +26,7 @@ #define PATCH_OBJECT_ABS 0x2002 #define PATCH_OBJECT_REL 0x2003 #define PATCH_OBJECT_ARM32_ABS 0x2004 +#define PATCH_OBJECT_ARM32_ABS_THM 0x2006 #define ENTRY_POINT 7 #define RUN_PROG 64 #define READ_DATA 65 From c7c8db633230abf7defb107a899c74d56973cf62 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 2 Mar 2026 21:28:46 +0100 Subject: [PATCH 09/22] R_ARM_THM_MOV* support added --- src/copapy/_compiler.py | 6 +++--- src/copapy/_stencils.py | 31 +++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/copapy/_compiler.py b/src/copapy/_compiler.py index cd42d41..156fbe9 100644 --- a/src/copapy/_compiler.py +++ b/src/copapy/_compiler.py @@ -393,14 +393,14 @@ def compile_to_dag(node_list: Iterable[Node], sdb: stencil_database) -> tuple[bi # assemble stencils to main program and patch stencils data = sdb.get_function_code('entry_function_shell', 'start') data_list.append(data) - print(f"* entry_function_shell (0) " + ' '.join(f'{d:02X}' for d in data)) + #print(f"* entry_function_shell (0) " + ' '.join(f'{d:02X}' for d in data)) offset = aux_func_len + len(data) for associated_net, node in extended_output_ops: assert node.name in sdb.stencil_definitions, f"- Warning: {node.name} stencil not found" data = sdb.get_stencil_code(node.name) data_list.append(data) - print(f"* {node.name} ({offset}) " + ' '.join(f'{d:02X}' for d in data)) + #print(f"* {node.name} ({offset}) " + ' '.join(f'{d:02X}' for d in data)) for reloc in sdb.get_relocations(node.name, stencil=True): if reloc.target_symbol_info in ('STT_OBJECT', 'STT_NOTYPE', 'STT_SECTION'): @@ -453,7 +453,7 @@ def compile_to_dag(node_list: Iterable[Node], sdb: stencil_database) -> tuple[bi for reloc in sdb.get_relocations(name): if not reloc.target_section_index: - assert reloc.pelfy_reloc.type == 'R_ARM_V4BX', (reloc.pelfy_reloc.type, name) + assert reloc.pelfy_reloc.type == 'R_ARM_V4BX', (reloc.pelfy_reloc.type, name, reloc.pelfy_reloc.symbol.name) elif reloc.target_symbol_info in {'STT_OBJECT', 'STT_NOTYPE', 'STT_SECTION'}: # Patch constants/variable addresses on heap diff --git a/src/copapy/_stencils.py b/src/copapy/_stencils.py index eb81aba..a317ea6 100644 --- a/src/copapy/_stencils.py +++ b/src/copapy/_stencils.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from typing import Generator, Literal, Iterable, TYPE_CHECKING import struct import platform +import os if TYPE_CHECKING: import pelfy @@ -46,6 +47,10 @@ class patch_entry: def detect_process_arch() -> str: + cp_target_arch = os.environ.get("CP_TARGET_ARCH") + if cp_target_arch: + return cp_target_arch + bits = struct.calcsize("P") * 8 arch = platform.machine().lower() @@ -305,21 +310,20 @@ class stencil_database(): scale = 8 #print(f" *> {patch_value=} {symbol_address=} {pr.fields['r_addend']=}, {function_offset=}") - elif pr.type.endswith('_MOVW_ABS_NC'): - # R_ARM_MOVW_ABS_NC + elif pr.type == 'R_ARM_MOVW_ABS_NC': # (S + A) & 0xFFFF mask = 0xFFFF patch_value = symbol_address + pr.fields['r_addend'] symbol_type = symbol_type + 0x04 # Absolut value #print(f" *> {pr.type} {patch_value=} {symbol_address=}, {function_offset=}") - elif pr.type.endswith('_MOVT_ABS'): - # R_ARM_MOVT_ABS + elif pr.type =='R_ARM_MOVT_ABS': # (S + A) & 0xFFFF0000 mask = 0xFFFF0000 patch_value = symbol_address + pr.fields['r_addend'] symbol_type = symbol_type + 0x04 # Absolut value scale = 0x10000 + #print(f" *> {pr.type} {patch_value=} {symbol_address=}, {function_offset=}, {pr.fields['r_addend']=}") elif pr.type.endswith('_ABS32'): # R_ARM_ABS32 @@ -330,9 +334,24 @@ class stencil_database(): elif pr.type.endswith('_THM_JUMP24') or pr.type.endswith('_THM_CALL'): # R_ARM_THM_JUMP24 # S + A - P - #assert pr.fields['r_addend'] == 0, pr.fields['r_addend'] - patch_value = symbol_address - (patch_offset + 4) #+ pr.fields['r_addend'] + patch_value = symbol_address - patch_offset + pr.fields['r_addend'] symbol_type = symbol_type + 0x05 # PATCH_FUNC_ARM32_THM + #print(f" *> {pr.type} {patch_value=} {symbol_address=} {pr.fields['r_addend']=} {pr.bits=}, {function_offset=} {patch_offset=}") + + elif pr.type == 'R_ARM_THM_MOVW_ABS_NC': + # (S + A) & 0xFFFF + mask = 0xFFFF + patch_value = symbol_address + pr.fields['r_addend'] + symbol_type = symbol_type + 0x06 # PATCH_OBJECT_ARM32_ABS_THM + #print(f" *> {pr.type} {patch_value=} {symbol_address=}, {function_offset=}, {pr.fields['r_addend']=}") + + elif pr.type == 'R_ARM_THM_MOVT_ABS': + # (S + A) & 0xFFFF0000 + mask = 0xFFFF0000 + patch_value = symbol_address + pr.fields['r_addend'] + symbol_type = symbol_type + 0x06 # PATCH_OBJECT_ARM32_ABS_THM + scale = 0x10000 + #print(f" *> {pr.type} {patch_value=} {symbol_address=}, {function_offset=}, {pr.fields['r_addend']=}") else: raise NotImplementedError(f"Relocation type {pr.type} in {relocation.pelfy_reloc.target_section.name} pointing to {relocation.pelfy_reloc.symbol.name} not implemented") From 2eb49cc2e5fddbb52bcd1d268e867ece07dc49cc Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 2 Mar 2026 21:29:46 +0100 Subject: [PATCH 10/22] test for ARM thumb updated --- ...s_armv7thunb.py => test_ops_armv7thumb.py} | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) rename tests/{test_ops_armv7thunb.py => test_ops_armv7thumb.py} (77%) diff --git a/tests/test_ops_armv7thunb.py b/tests/test_ops_armv7thumb.py similarity index 77% rename from tests/test_ops_armv7thunb.py rename to tests/test_ops_armv7thumb.py index ac67e51..983510e 100644 --- a/tests/test_ops_armv7thunb.py +++ b/tests/test_ops_armv7thumb.py @@ -55,6 +55,9 @@ def function1(c1: NumLike) -> list[NumLike]: c1 + 4, c1 - 4, c1 > 2, c1 > 100, c1 < 4, c1 < 100] +def function1ex(c1: NumLike) -> list[NumLike]: + return [c1 // 4] + def function2(c1: NumLike) -> list[NumLike]: return [c1 * 4.44, c1 * -4.44] @@ -90,24 +93,21 @@ def test_compile(): c_f = value(1.111) c_b = value(True) - #ret_test = function1(c_i) + function1(c_f) + function2(c_i) + function2(c_f) + function3(c_i) + function4(c_i) + function5(c_b) + [value(9) % 2] + iiftests(c_i) + iiftests(c_f) + [cp.asin(c_i/10)] - #ret_ref = function1(9) + function1(1.111) + function2(9) + function2(1.111) + function3(9) + function4(9) + function5(True) + [9 % 2] + iiftests(9) + iiftests(1.111) + [cp.asin(9/10)] - - ret_test = (c_i * 100 + 5,) - ret_ref = (9 * 100 + 5,) + ret_test = function1(c_i) + function1(c_f) + function2(c_i) + function2(c_f) + function3(c_i) + function4(c_i) + function5(c_b) + [value(9) % 2] + iiftests(c_i) + iiftests(c_f) + [cp.asin(c_i/10)] + ret_ref = function1(9) + function1(1.111) + function2(9) + function2(1.111) + function3(9) + function4(9) + function5(True) + [9 % 2] + iiftests(9) + iiftests(1.111) + [cp.asin(9/10)] out = [Store(r) for r in ret_test] - sdb = backend.stencil_db_from_package('armv7thumb') + sdb = backend.stencil_db_from_package('armv7mthumb') dw, variables = compile_to_dag(out, sdb) #dw.write_com(_binwrite.Command.READ_DATA) #dw.write_int(0) #dw.write_int(28) - # run program command - #dw.write_com(_binwrite.Command.RUN_PROG) - dw.write_com(_binwrite.Command.DUMP_CODE) + du = dw.copy() + dw.write_com(_binwrite.Command.RUN_PROG) + du.write_com(_binwrite.Command.DUMP_CODE) for v in ret_test: assert isinstance(v, value) @@ -118,11 +118,13 @@ def test_compile(): #dw.write_int(28) dw.write_com(_binwrite.Command.END_COM) + du.write_com(_binwrite.Command.END_COM) #print('* Data to runner:') #dw.print() dw.to_file('build/runner/test-armv7thumb.copapy') + du.to_file('build/runner/test-armv7thumb-dump.copapy') if not check_for_qemu(): warnings.warn("qemu-armv7 not found, armv7 test skipped!", UserWarning) @@ -131,12 +133,14 @@ def test_compile(): warnings.warn("armv7thumb runner not found, armv7thumb test skipped!", UserWarning) return - command = qemu_command + ['build/runner/coparun-armv7thumb', 'build/runner/test-armv7thumb.copapy'] + ['build/runner/test.copapy-armv7thumb.bin'] - #try: + print('----- Dump code...') + command = qemu_command + ['build/runner/coparun-armv7thumb', 'build/runner/test-armv7thumb-dump.copapy', 'build/runner/test.copapy-armv7thumb.bin'] result = run_command(command) - #except FileNotFoundError: - # warnings.warn(f"Test skipped, executable not found.", UserWarning) - # return + + print('----- Run code...') + command = qemu_command + ['build/runner/coparun-armv7thumb', 'build/runner/test-armv7thumb.copapy'] + result = run_command(command) + print('* Output from runner:\n--') print(result) @@ -167,5 +171,14 @@ def test_compile(): if __name__ == "__main__": - #test_example() - test_slow_31bit_int_list_hash() + test_compile() + + +""" +qemu-arm -d in_asm,exec,cpu_reset -D qemu.log build/runner/coparun-armv7thumb build/runner/test-armv7thumb.copapy build/runner/test.copapy-armv7thumb.bin + +qemu-arm -d in_asm,exec -D qemu_trace.log \ + -global driver=pl011.audiomaddr,property=addr,value=0xff7ec000 \ + -global driver=pl011.audiomaddr,property=size,value=0x100000 \ + your_binary +""" \ No newline at end of file From accb03f042c049ca78d07546f84c878ae2cdea9e Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 2 Mar 2026 21:31:07 +0100 Subject: [PATCH 11/22] Fix in test function "get_42" --- src/copapy/_math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/copapy/_math.py b/src/copapy/_math.py index dd26bf6..fcd14c9 100644 --- a/src/copapy/_math.py +++ b/src/copapy/_math.py @@ -310,7 +310,7 @@ def get_42(x: value[Any]) -> value[float]: ... def get_42(x: NumLike) -> value[float] | float: """Returns the value representing the constant 42""" if isinstance(x, value): - return add_op('get_42', [x, x]) + return add_op('get_42', [x]) return float((int(x) * 3.0 + 42.0) * 5.0 + 21.0) From d2069d5d07b9b3268c9e12d6e92fc7f8c2d35619 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 2 Mar 2026 21:32:12 +0100 Subject: [PATCH 12/22] build script for local stencil builds updated for ARM64, ARM-Thumb and ARM-CortexM-Thumb --- tools/build.sh | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/tools/build.sh b/tools/build.sh index 4af186a..28679fd 100644 --- a/tools/build.sh +++ b/tools/build.sh @@ -4,10 +4,10 @@ set -eu ARCH=${1:-x86_64} case "$ARCH" in - (x86_64|arm-v6|arm-v7|arm-v7-thumb|arm-v7m-thumb|all) + (x86_64|arm64|arm-v6|arm-v7|arm-v7-thumb|arm-v7m-thumb|all) ;; (*) - echo "Usage: $0 [x86_64|arm-v6|arm-v7|arm-v6-thumb|arm-v7m-thumb|all]" + echo "Usage: $0 [x86_64|arm64|arm-v6|arm-v7|arm-v6-thumb|arm-v7m-thumb|all]" exit 1 ;; esac @@ -42,13 +42,44 @@ if [[ "$ARCH" == "x86_64" || "$ARCH" == "all" ]]; then -o build/runner/coparun fi +####################################### +# ARM 64 +####################################### +if [[ "$ARCH" == "arm64" || "$ARCH" == "all" ]]; then + echo "--------------arm64----------------" + + LIBGCC=$(aarch64-linux-gnu-gcc -print-libgcc-file-name) + + aarch64-linux-gnu-gcc -fno-pic -ffunction-sections \ + -c $SRC -O3 -o build/stencils/stencils.o + + aarch64-linux-gnu-ld -r \ + build/stencils/stencils.o \ + build/musl/musl_objects_arm64.o \ + $LIBGCC \ + -o $DEST/stencils_arm64_O3.o + + aarch64-linux-gnu-objdump -d -x \ + $DEST/stencils_arm64_O3.o \ + > build/stencils/stencils_arm64_O3.asm + + aarch64-linux-gnu-gcc \ + -Wall -Wextra -Wconversion -Wsign-conversion -static \ + -Wshadow -Wstrict-overflow -O3 \ + -DENABLE_LOGGING \ + src/coparun/runmem.c \ + src/coparun/coparun.c \ + src/coparun/mem_man.c \ + -o build/runner/coparun-arm64 +fi + ####################################### # ARM v6 ####################################### if [[ "$ARCH" == "arm-v6" || "$ARCH" == "all" ]]; then echo "--------------arm-v6 32 bit----------------" - LIBGCC=$(arm-none-eabi-gcc -print-libgcc-file-name) + LIBGCC=$(arm-none-eabi-gcc -march=armv6 -mfpu=vfp -mfloat-abi=hard -marm -print-libgcc-file-name) arm-none-eabi-gcc -fno-pic -ffunction-sections \ -march=armv6 -mfpu=vfp -mfloat-abi=hard -marm \ @@ -81,7 +112,7 @@ fi if [[ "$ARCH" == "arm-v7" || "$ARCH" == "all" ]]; then echo "--------------arm-v7 32 bit----------------" - LIBGCC=$(arm-none-eabi-gcc -print-libgcc-file-name) + LIBGCC=$(arm-none-eabi-gcc -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -marm -print-libgcc-file-name) arm-none-eabi-gcc -fno-pic -ffunction-sections \ -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -marm \ @@ -114,7 +145,7 @@ fi if [[ "$ARCH" == "arm-v7-thumb" || "$ARCH" == "all" ]]; then echo "--------------arm-v7a-thumb 32 bit----------------" - LIBGCC=$(arm-none-eabi-gcc -print-libgcc-file-name) + LIBGCC=$(arm-none-eabi-gcc -march=armv7 -mfpu=vfp3 -mthumb -print-libgcc-file-name) arm-none-eabi-gcc -fno-pic -ffunction-sections \ -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -mthumb \ @@ -122,7 +153,7 @@ if [[ "$ARCH" == "arm-v7-thumb" || "$ARCH" == "all" ]]; then arm-none-eabi-ld -r \ build/stencils/stencils.o \ - build/musl/musl_objects_armv7.o \ + build/musl/musl_objects_armv7thumb.o \ $LIBGCC \ -o $DEST/stencils_armv7thumb_O3.o @@ -147,7 +178,7 @@ fi if [[ "$ARCH" == "arm-v7m-thumb" || "$ARCH" == "all" ]]; then echo "--------------arm-v7m-thumb 32 bit----------------" - LIBGCC=$(arm-none-eabi-gcc -print-libgcc-file-name) + LIBGCC=$(arm-none-eabi-gcc -march=armv7e-m -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -print-libgcc-file-name) arm-none-eabi-gcc -fno-pic -ffunction-sections \ -march=armv7e-m -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb \ From dd7fb12c64e4ecbbef6b39955ed02ca8e62e1a5b Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 2 Mar 2026 21:32:45 +0100 Subject: [PATCH 13/22] Helper bash script added for debugging ARM thumb stencils --- tools/test_thumb_stancils.sh | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100755 tools/test_thumb_stancils.sh diff --git a/tools/test_thumb_stancils.sh b/tools/test_thumb_stancils.sh new file mode 100755 index 0000000..7fec81e --- /dev/null +++ b/tools/test_thumb_stancils.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +bash tools/build.sh arm-v7-thumb +python tests/test_ops_armv7thumb.py +qemu-arm -d in_asm -D qemu.log build/runner/coparun-armv7thumb build/runner/test-armv7thumb.copapy build/runner/test.copapy-armv7thumb.bin +arm-none-eabi-objdump -D -b binary -marm -M force-thumb --adjust-vma=0xff7ed000 build/runner/test.copapy-armv7thumb.bin > build/runner/test.copapy-armv7thumb.asm \ No newline at end of file From 7f963d7e43d0ca555e01aee6c79bcc2d5be30ec9 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 2 Mar 2026 21:33:24 +0100 Subject: [PATCH 14/22] CI: Testing for ARMv7 extended to armv7thumb and armv7mthumb --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 349f366..9f6864e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -214,6 +214,10 @@ jobs: gcc -O3 -static -DENABLE_LOGGING -o build/runner/coparun src/coparun/runmem.c \ src/coparun/coparun.c src/coparun/mem_man.c && \ pytest && \ + export CP_TARGET_ARCH=armv7thumb && \ + pytest && \ + export CP_TARGET_ARCH=armv7mthumb && \ + pytest && \ bash tools/create_asm.sh" - uses: actions/upload-artifact@v4 From 7a3088ec48a8ddf95792d0ec66f9c64c06b291fc Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 3 Mar 2026 13:05:47 +0100 Subject: [PATCH 15/22] added add_sign_int32 function, since pelfy returns addend-values for 32 bit x86 as unsigned int32 --- src/copapy/_stencils.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/copapy/_stencils.py b/src/copapy/_stencils.py index f9bd866..001d15d 100644 --- a/src/copapy/_stencils.py +++ b/src/copapy/_stencils.py @@ -135,6 +135,11 @@ def get_op_after_last_call_in_function(func: pelfy.elf_symbol) -> int: return reloc.fields['r_offset'] - func.offset_in_section + 4 +def add_sign_int32(value: int) -> int: + """Convert a 32-bit unsigned integer to a signed integer.""" + return value - 0x100000000 if value > 0x7FFFFFFF else value + + class stencil_database(): """A class for loading and querying a stencil database from an ELF object file @@ -226,6 +231,7 @@ class stencil_database(): cache.append(reloc_entry) yield reloc_entry + def get_patch(self, relocation: relocation_entry, symbol_address: int, function_offset: int, symbol_type: int) -> patch_entry: """Return patch positions for a provided symbol (function or object) @@ -251,12 +257,14 @@ class stencil_database(): if pr.type.endswith('64_PC32') or pr.type.endswith('64_PLT32'): # S + A - P - patch_value = symbol_address + pr.fields['r_addend'] - patch_offset + addend = add_sign_int32(pr.fields['r_addend']) + patch_value = symbol_address + addend - patch_offset #print(f" *> {pr.type} {patch_value=} {symbol_address=} {pr.fields['r_addend']=} {pr.bits=}, {function_offset=} {patch_offset=}") elif pr.type == 'R_386_PC32': # S + A - P - patch_value = symbol_address + pr.fields['r_addend'] - patch_offset + addend = add_sign_int32(pr.fields['r_addend']) + patch_value = symbol_address + addend - patch_offset #print(f" *> {pr.type} {pr.symbol.name} {patch_value=} {symbol_address=} {pr.fields['r_addend']=} {bin(pr.fields['r_addend'])} {pr.bits=}, {function_offset=} {patch_offset=}") elif pr.type == 'R_386_32': From 04cdf50a04f7ec62a7d08b2de93faa217230307b Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 3 Mar 2026 13:24:46 +0100 Subject: [PATCH 16/22] Updated pelfy dependency --- .github/workflows/ci.yml | 8 ++++---- pyproject.toml | 18 +++++++++++------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9f6864e..e1c3203 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -151,7 +151,7 @@ jobs: - name: Use ARM64 container run: | docker run --rm -v $PWD:/app -w /app --platform linux/arm64 ghcr.io/nonannet/arm64_test:1 \ - bash -lc "pip install . && \ + bash -lc "pip install .[mindev] && \ mkdir -p build/runner && \ gcc -O3 -static -DENABLE_LOGGING -o build/runner/coparun src/coparun/runmem.c \ src/coparun/coparun.c src/coparun/mem_man.c && \ @@ -180,7 +180,7 @@ jobs: - name: Use ARMv6 container run: | docker run --rm -v $PWD:/app -w /app --platform linux/arm/v6 ghcr.io/nonannet/armv6_test:1 \ - bash -lc "pip install . && \ + bash -lc "pip install .[mindev] && \ mkdir -p build/runner && \ gcc -O3 -static -DENABLE_LOGGING -o build/runner/coparun src/coparun/runmem.c \ src/coparun/coparun.c src/coparun/mem_man.c && \ @@ -209,7 +209,7 @@ jobs: - name: Use ARMv7 container run: | docker run --rm -v $PWD:/app -w /app --platform linux/arm/v7 ghcr.io/nonannet/armv7_test:1 \ - bash -lc "pip install . && \ + bash -lc "pip install .[mindev] && \ mkdir -p build/runner && \ gcc -O3 -static -DENABLE_LOGGING -o build/runner/coparun src/coparun/runmem.c \ src/coparun/coparun.c src/coparun/mem_man.c && \ @@ -248,7 +248,7 @@ jobs: python-version: ${{ matrix.python-version }} - name: Install Python dependencies - run: python -m pip install .[dev] + run: python -m pip install .[mindev] - name: Set up MSVC environment uses: microsoft/setup-msbuild@v2 diff --git a/pyproject.toml b/pyproject.toml index d5643c8..1ee53ec 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ name = "copapy" dynamic = ["version"] authors = [ - { name="Nicolas Kruse", email="nicolas.kruse@nonan.net" }, + { name="Nicolas Kruse", email="nicolas.kruse@nonan.net" }, ] description = "Copy-Patch Compiler" readme = "README.md" @@ -45,14 +45,18 @@ dev = [ "ruff", "mypy", "pytest", - "pelfy>=1.0.7" + "pelfy>=1.0.8" +] +mindev = [ + "pytest", + "pelfy>=1.0.8" ] doc_build = [ - "sphinx", - "pydata_sphinx_theme", - "sphinx-autodoc-typehints", - "myst-parser", - "pelfy>=1.0.7" + "sphinx", + "pydata_sphinx_theme", + "sphinx-autodoc-typehints", + "myst-parser", + "pelfy>=1.0.8" ] [tool.mypy] From c31601853b19ffd0f997081227e5564806fdfcd8 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 3 Mar 2026 13:31:22 +0100 Subject: [PATCH 17/22] CI: LIBGCC version fixed for thumb builds --- tools/crosscompile.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/crosscompile.sh b/tools/crosscompile.sh index 29a15ca..78a4f2d 100644 --- a/tools/crosscompile.sh +++ b/tools/crosscompile.sh @@ -43,12 +43,12 @@ arm-none-eabi-ld -r $STMP /object_files/musl_objects_armv7.o $LIBGCC -o $DEST/st # ARMv7 Thumb for Cortex-A with hardware fp arm-none-eabi-gcc -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -mthumb $FLAGS -$OPT -c $SRC -o $STMP -LIBGCC=$(arm-none-eabi-gcc -print-libgcc-file-name) +LIBGCC=$(arm-none-eabi-gcc -march=armv7 -mfpu=vfp3 -mthumb -print-libgcc-file-name) arm-none-eabi-ld -r $STMP /object_files/musl_objects_armv7thumb.o $LIBGCC -o $DEST/stencils_armv7thumb_$OPT.o # Armv7 Thumb for Cortex-M3..7 hardware fp arm-none-eabi-gcc -march=armv7e-m -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb $FLAGS -$OPT -c $SRC -o $STMP -LIBGCC=$(arm-none-eabi-gcc -print-libgcc-file-name) +LIBGCC=$(arm-none-eabi-gcc -march=armv7e-m -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -print-libgcc-file-name) arm-none-eabi-ld -r $STMP /object_files/musl_objects_armv7mthumb.o $LIBGCC -o $DEST/stencils_armv7mthumb_$OPT.o # PowerPC64LE From 0212fa77a3d5f1b94db27d35fedd15aa601a7a25 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 3 Mar 2026 14:25:50 +0100 Subject: [PATCH 18/22] type annotation for data_writer.copy() fixed --- src/copapy/_binwrite.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/copapy/_binwrite.py b/src/copapy/_binwrite.py index 1af2ee3..db1f15f 100644 --- a/src/copapy/_binwrite.py +++ b/src/copapy/_binwrite.py @@ -25,7 +25,7 @@ class data_writer(): self._data: list[tuple[str, bytes, int]] = [] self.byteorder: ByteOrder = byteorder - def copy(self): + def copy(self) -> 'data_writer': cp = data_writer(self.byteorder) cp._data = self._data.copy() return cp From b7d5f3a1297bd9812138066d64d18f6224054670 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 3 Mar 2026 14:47:53 +0100 Subject: [PATCH 19/22] exclude non-stencil functions in the test "test_start_end_function" --- tests/test_stencil_db.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/test_stencil_db.py b/tests/test_stencil_db.py index 0ef40e4..7e762bd 100644 --- a/tests/test_stencil_db.py +++ b/tests/test_stencil_db.py @@ -19,11 +19,18 @@ def test_start_end_function(): if symbol.relocations and symbol.relocations[-1].symbol.info == 'STT_NOTYPE': - print('-', sym_name, get_stencil_position(symbol), len(symbol.data)) + if symbol.section and symbol.section.name == '.text': + print('SKIP', sym_name, '(Aux function, not a stencil)') + continue - start, end = get_stencil_position(symbol) + if symbol.section: + function_size = symbol.section.fields['sh_size'] # len(symbol.data) excludes nop after the function - assert start >= 0 and end >= start and end <= len(symbol.data) + print('-', sym_name, get_stencil_position(symbol), function_size) + + start, end = get_stencil_position(symbol) + + assert (start >= 0 and end >= start and end <= function_size) def test_aux_functions(): From c6fd69d61b112878d57b87bfb4022f0300fc5b65 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 3 Mar 2026 15:34:37 +0100 Subject: [PATCH 20/22] CI: different armv7 variates separated --- .github/workflows/ci.yml | 80 +++++++++++++++++++++++++++++++++++++--- tools/build.sh | 7 +++- 2 files changed, 80 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e1c3203..1cd9751 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -211,13 +211,13 @@ jobs: docker run --rm -v $PWD:/app -w /app --platform linux/arm/v7 ghcr.io/nonannet/armv7_test:1 \ bash -lc "pip install .[mindev] && \ mkdir -p build/runner && \ - gcc -O3 -static -DENABLE_LOGGING -o build/runner/coparun src/coparun/runmem.c \ + gcc -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -marm -static \ + -Wall -Wextra -Wconversion -Wsign-conversion \ + -Wshadow -Wstrict-overflow -O3 \ + -DENABLE_LOGGING \ + -o build/runner/coparun src/coparun/runmem.c \ src/coparun/coparun.c src/coparun/mem_man.c && \ pytest && \ - export CP_TARGET_ARCH=armv7thumb && \ - pytest && \ - export CP_TARGET_ARCH=armv7mthumb && \ - pytest && \ bash tools/create_asm.sh" - uses: actions/upload-artifact@v4 @@ -225,6 +225,74 @@ jobs: name: runner-linux-armv7 path: build/runner/* + build-armv7thumb: + needs: [build_stencils] + runs-on: ubuntu-latest + continue-on-error: true + steps: + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 + with: + name: stencil-object-files + path: src/copapy/obj + - name: Set up QEMU for ARMv7 + uses: docker/setup-qemu-action@v3 + with: + platforms: linux/arm/v7 + - name: Use ARMv7 container + run: | + docker run --rm -v $PWD:/app -w /app --platform linux/arm/v7 ghcr.io/nonannet/armv7_test:1 \ + bash -lc "pip install .[mindev] && \ + mkdir -p build/runner && \ + gcc -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -marm -static \ + -Wall -Wextra -Wconversion -Wsign-conversion \ + -Wshadow -Wstrict-overflow -O3 \ + -DENABLE_LOGGING \ + -o build/runner/coparun src/coparun/runmem.c \ + src/coparun/coparun.c src/coparun/mem_man.c && \ + export CP_TARGET_ARCH=armv7thumb && \ + pytest && \ + bash tools/create_asm.sh" + + - uses: actions/upload-artifact@v4 + with: + name: runner-linux-armv7thumb + path: build/runner/* + + build-armv7mthumb: + needs: [build_stencils] + runs-on: ubuntu-latest + continue-on-error: true + steps: + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 + with: + name: stencil-object-files + path: src/copapy/obj + - name: Set up QEMU for ARMv7 + uses: docker/setup-qemu-action@v3 + with: + platforms: linux/arm/v7 + - name: Use ARMv7 container + run: | + docker run --rm -v $PWD:/app -w /app --platform linux/arm/v7 ghcr.io/nonannet/armv7_test:1 \ + bash -lc "pip install .[mindev] && \ + mkdir -p build/runner && \ + gcc -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -marm -static \ + -Wall -Wextra -Wconversion -Wsign-conversion \ + -Wshadow -Wstrict-overflow -O3 \ + -DENABLE_LOGGING \ + -o build/runner/coparun src/coparun/runmem.c \ + src/coparun/coparun.c src/coparun/mem_man.c && \ + export CP_TARGET_ARCH=armv7mthumb && \ + pytest && \ + bash tools/create_asm.sh" + + - uses: actions/upload-artifact@v4 + with: + name: runner-linux-armv7mthumb + path: build/runner/* + build-windows: needs: [build_stencils] runs-on: windows-latest @@ -307,6 +375,8 @@ jobs: cp tmp/runner-linux-arm64/coparun release/coparun-aarch64 cp tmp/runner-linux-armv6/coparun release/coparun-armv6 cp tmp/runner-linux-armv7/coparun release/coparun-armv7 + cp tmp/runner-linux-armv7thumb/coparun release/coparun-armv7thumb + cp tmp/runner-linux-armv7mthumb/coparun release/coparun-armv7mthumb cp tmp/runner-win/coparun*.exe release/ TAG="${{ steps.version.outputs.version }}" diff --git a/tools/build.sh b/tools/build.sh index 28679fd..4686e3d 100644 --- a/tools/build.sh +++ b/tools/build.sh @@ -128,6 +128,7 @@ if [[ "$ARCH" == "arm-v7" || "$ARCH" == "all" ]]; then $DEST/stencils_armv7_O3.o \ > build/stencils/stencils_armv7_O3.asm + # The same runner for all ARM7 arm-linux-gnueabihf-gcc \ -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -marm -static \ -Wall -Wextra -Wconversion -Wsign-conversion \ @@ -161,8 +162,9 @@ if [[ "$ARCH" == "arm-v7-thumb" || "$ARCH" == "all" ]]; then $DEST/stencils_armv7thumb_O3.o \ > build/stencils/stencils_armv7thumb_O3.asm + # The same runner for all ARM7 arm-linux-gnueabihf-gcc \ - -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -mthumb -static \ + -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -static \ -Wall -Wextra -Wconversion -Wsign-conversion \ -Wshadow -Wstrict-overflow -O3 \ -DENABLE_LOGGING \ @@ -194,8 +196,9 @@ if [[ "$ARCH" == "arm-v7m-thumb" || "$ARCH" == "all" ]]; then $DEST/stencils_armv7mthumb_O3.o \ > build/stencils/stencils_armv7mthumb_O3.asm + # The same runner for all ARM7 arm-linux-gnueabihf-gcc \ - -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -mthumb -static \ + -march=armv7-a -mfpu=neon-vfpv3 -mfloat-abi=hard -static \ -Wall -Wextra -Wconversion -Wsign-conversion \ -Wshadow -Wstrict-overflow -O3 \ -DENABLE_LOGGING \ From e48fc7c485d416b2566fcb10d1badf02fcd11b83 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 3 Mar 2026 16:09:06 +0100 Subject: [PATCH 21/22] test for arm thumb variants split into two tests --- tests/test_ops_armv7mthumb.py | 174 ++++++++++++++++++++++++++++++++++ tests/test_ops_armv7thumb.py | 10 +- 2 files changed, 179 insertions(+), 5 deletions(-) create mode 100644 tests/test_ops_armv7mthumb.py diff --git a/tests/test_ops_armv7mthumb.py b/tests/test_ops_armv7mthumb.py new file mode 100644 index 0000000..16058d6 --- /dev/null +++ b/tests/test_ops_armv7mthumb.py @@ -0,0 +1,174 @@ +from copapy import NumLike, iif, value +from copapy.backend import Store, compile_to_dag, add_read_value_remote +import subprocess +from copapy import _binwrite +import copapy.backend as backend +import os +import warnings +import re +import struct +import pytest +import copapy as cp + +if os.name == 'nt': + # On Windows wsl and qemu-user is required: + # sudo apt install qemu-user + qemu_command = ['wsl', 'qemu-arm'] +else: + qemu_command = ['qemu-arm'] + + +def parse_results(log_text: str) -> dict[int, bytes]: + regex = r"^READ_DATA offs=(\d*) size=(\d*) data=(.*)$" + matches = re.finditer(regex, log_text, re.MULTILINE) + var_dict: dict[int, bytes] = {} + + for match in matches: + value_str: list[str] = match.group(3).strip().split(' ') + #print('--', value_str) + value = bytes(int(v, base=16) for v in value_str) + if len(value) <= 8: + var_dict[int(match.group(1))] = value + + return var_dict + + +def run_command(command: list[str]) -> str: + result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf8', check=False) + assert result.returncode != 11, f"SIGSEGV (segmentation fault)\n -Error occurred: {result.stderr}\n -Output: {result.stdout}" + assert result.returncode == 0, f"\n -Error occurred: {result.stderr}\n -Output: {result.stdout}" + return result.stdout + + +def check_for_qemu() -> bool: + command = qemu_command + ['--version'] + try: + result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) + except Exception: + return False + return result.returncode == 0 + + +def function1(c1: NumLike) -> list[NumLike]: + return [c1 / 4, c1 / -4, c1 // 4, c1 // -4, (c1 * -1) // 4, + c1 * 4, c1 * -4, + c1 + 4, c1 - 4, + c1 > 2, c1 > 100, c1 < 4, c1 < 100] + +def function1ex(c1: NumLike) -> list[NumLike]: + return [c1 // 4] + + +def function2(c1: NumLike) -> list[NumLike]: + return [c1 * 4.44, c1 * -4.44] + + +def function3(c1: NumLike) -> list[NumLike]: + return [c1 / 4] + + +def function4(c1: NumLike) -> list[NumLike]: + return [c1 == 9, c1 == 4, c1 != 9, c1 != 4] + + +def function5(c1: NumLike) -> list[NumLike]: + return [c1 == True, c1 == False, c1 != True, c1 != False, c1 / 2, c1 + 2] + + +def function6(c1: NumLike) -> list[NumLike]: + return [c1 == True] + + +def iiftests(c1: NumLike) -> list[NumLike]: + return [iif(c1 > 5, 8, 9), + iif(c1 < 5, 8.5, 9.5), + iif(1 > 5, 3.3, 8.8) + c1, + iif(1 < 5, c1 * 3.3, 8.8), + iif(c1 < 5, c1 * 3.3, 8.8)] + + +@pytest.mark.runner +def test_compile(): + c_i = value(9) + c_f = value(1.111) + c_b = value(True) + + ret_test = function1(c_i) + function1(c_f) + function2(c_i) + function2(c_f) + function3(c_i) + function4(c_i) + function5(c_b) + [value(9) % 2] + iiftests(c_i) + iiftests(c_f) + [cp.asin(c_i/10)] + ret_ref = function1(9) + function1(1.111) + function2(9) + function2(1.111) + function3(9) + function4(9) + function5(True) + [9 % 2] + iiftests(9) + iiftests(1.111) + [cp.asin(9/10)] + + out = [Store(r) for r in ret_test] + + sdb = backend.stencil_db_from_package('armv7mthumb') + dw, variables = compile_to_dag(out, sdb) + + #dw.write_com(_binwrite.Command.READ_DATA) + #dw.write_int(0) + #dw.write_int(28) + + du = dw.copy() + dw.write_com(_binwrite.Command.RUN_PROG) + du.write_com(_binwrite.Command.DUMP_CODE) + + for v in ret_test: + assert isinstance(v, value) + add_read_value_remote(dw, variables, v.net) + + #dw.write_com(_binwrite.Command.READ_DATA) + #dw.write_int(0) + #dw.write_int(28) + + dw.write_com(_binwrite.Command.END_COM) + du.write_com(_binwrite.Command.END_COM) + + #print('* Data to runner:') + #dw.print() + + dw.to_file('build/runner/test-armv7mthumb.copapy') + du.to_file('build/runner/test-armv7mthumb-dump.copapy') + + if not check_for_qemu(): + warnings.warn("qemu-armv7 not found, armv7 test skipped!", UserWarning) + return + if not os.path.isfile('build/runner/coparun-armv7'): + warnings.warn("armv7 runner not found, armv7 test skipped!", UserWarning) + return + + print('----- Dump code...') + command = qemu_command + ['build/runner/coparun-armv7', 'build/runner/test-armv7mthumb-dump.copapy', 'build/runner/test.copapy-armv7mthumb.bin'] + result = run_command(command) + + print('----- Run code...') + command = qemu_command + ['build/runner/coparun-armv7', 'build/runner/test-armv7mthumb.copapy'] + result = run_command(command) + + + print('* Output from runner:\n--') + print(result) + print('--') + + assert 'Return value: 1' in result + + result_data = parse_results(result) + + for test, ref in zip(ret_test, ret_ref): + assert isinstance(test, value) + address = variables[test.net][0] + data = result_data[address] + if test.dtype == 'int': + val = int.from_bytes(data, sdb.byteorder, signed=True) + elif test.dtype == 'bool': + val = bool.from_bytes(data, sdb.byteorder) + elif test.dtype == 'float': + en = {'little': '<', 'big': '>'}[sdb.byteorder] + val = struct.unpack(en + 'f', data)[0] + assert isinstance(val, float) + else: + raise Exception(f"Unknown type: {test.dtype}") + print('+', val, ref, test.dtype, f" addr={address}") + for t in (int, float, bool): + assert isinstance(val, t) == isinstance(ref, t), f"Result type does not match for {val} and {ref}" + assert val == pytest.approx(ref, 1e-5), f"Result does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType] + + +if __name__ == "__main__": + test_compile() diff --git a/tests/test_ops_armv7thumb.py b/tests/test_ops_armv7thumb.py index 983510e..997f1cd 100644 --- a/tests/test_ops_armv7thumb.py +++ b/tests/test_ops_armv7thumb.py @@ -98,7 +98,7 @@ def test_compile(): out = [Store(r) for r in ret_test] - sdb = backend.stencil_db_from_package('armv7mthumb') + sdb = backend.stencil_db_from_package('armv7thumb') dw, variables = compile_to_dag(out, sdb) #dw.write_com(_binwrite.Command.READ_DATA) @@ -129,16 +129,16 @@ def test_compile(): if not check_for_qemu(): warnings.warn("qemu-armv7 not found, armv7 test skipped!", UserWarning) return - if not os.path.isfile('build/runner/coparun-armv7thumb'): - warnings.warn("armv7thumb runner not found, armv7thumb test skipped!", UserWarning) + if not os.path.isfile('build/runner/coparun-armv7'): + warnings.warn("armv7 runner not found, armv7 test skipped!", UserWarning) return print('----- Dump code...') - command = qemu_command + ['build/runner/coparun-armv7thumb', 'build/runner/test-armv7thumb-dump.copapy', 'build/runner/test.copapy-armv7thumb.bin'] + command = qemu_command + ['build/runner/coparun-armv7', 'build/runner/test-armv7thumb-dump.copapy', 'build/runner/test.copapy-armv7thumb.bin'] result = run_command(command) print('----- Run code...') - command = qemu_command + ['build/runner/coparun-armv7thumb', 'build/runner/test-armv7thumb.copapy'] + command = qemu_command + ['build/runner/coparun-armv7', 'build/runner/test-armv7thumb.copapy'] result = run_command(command) From a4410fd359c1285c874281d7a23ee272c032c8c2 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 3 Mar 2026 16:56:59 +0100 Subject: [PATCH 22/22] CI: exclude runner for arm thumb --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1cd9751..fb268d9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -251,7 +251,7 @@ jobs: -o build/runner/coparun src/coparun/runmem.c \ src/coparun/coparun.c src/coparun/mem_man.c && \ export CP_TARGET_ARCH=armv7thumb && \ - pytest && \ + pytest -m 'not runner' && \ bash tools/create_asm.sh" - uses: actions/upload-artifact@v4 @@ -285,7 +285,7 @@ jobs: -o build/runner/coparun src/coparun/runmem.c \ src/coparun/coparun.c src/coparun/mem_man.c && \ export CP_TARGET_ARCH=armv7mthumb && \ - pytest && \ + pytest -m 'not runner' && \ bash tools/create_asm.sh" - uses: actions/upload-artifact@v4