From 45c7fa5b3b3589de6954003fda8528be30326f67 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Wed, 8 Oct 2025 22:59:51 +0200 Subject: [PATCH] file names adjusted --- .github/workflows/build_wheels.yml | 2 +- .github/workflows/ci.yml | 2 +- .gitignore | 2 - inspect.sh | 6 - src/copapy/stencils.c | 291 +++++++++++++++++++++ test.sh | 7 - build.bat => tools/build.bat | 0 build.sh => tools/build.sh | 4 +- {src/copapy/obj => tools}/crosscompile.sh | 6 +- tools/extract_code.py | 126 ++++----- {src/copapy => tools}/generate_stencils.py | 0 tools/inspect.sh | 6 + {src/copapy/obj => tools}/nativecompile.sh | 6 +- 13 files changed, 373 insertions(+), 85 deletions(-) delete mode 100644 inspect.sh create mode 100644 src/copapy/stencils.c delete mode 100644 test.sh rename build.bat => tools/build.bat (100%) rename build.sh => tools/build.sh (89%) rename {src/copapy/obj => tools}/crosscompile.sh (88%) rename {src/copapy => tools}/generate_stencils.py (100%) create mode 100644 tools/inspect.sh rename {src/copapy/obj => tools}/nativecompile.sh (70%) diff --git a/.github/workflows/build_wheels.yml b/.github/workflows/build_wheels.yml index 9743e53..cd012e9 100644 --- a/.github/workflows/build_wheels.yml +++ b/.github/workflows/build_wheels.yml @@ -22,7 +22,7 @@ jobs: gcc-12-riscv64-linux-gnu - name: Build object files - run: bash src/copapy/obj/crosscompile.sh + run: bash tools/crosscompile.sh - uses: actions/upload-artifact@v4 with: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2f15b97..91a253c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,7 @@ jobs: - uses: actions/checkout@v4 - name: Build object files - run: bash src/copapy/obj/nativecompile.sh + run: bash tools/nativecompile.sh - uses: actions/upload-artifact@v4 with: diff --git a/.gitignore b/.gitignore index 2c102f2..85770e0 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,6 @@ __pycache__ .pytest_cache /tests/autogenerated_*.py *test*.o -/bin/* token.txt /src/copapy/obj/*.o /src/copapy/obj/old/*.o @@ -20,5 +19,4 @@ bin/* build/* /*.obj /src/*.pyd -src/copapy/stencils.c vc140.pdb diff --git a/inspect.sh b/inspect.sh deleted file mode 100644 index 72e7fb5..0000000 --- a/inspect.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -source build.sh -python tests/test_compile_div.py -python tools/extract_code.py -objdump -D -b binary -m i386:x86-64 --adjust-vma=0x1000 bin/test_code.bin > bin/test_code.bin.txt diff --git a/src/copapy/stencils.c b/src/copapy/stencils.c new file mode 100644 index 0000000..7c2e316 --- /dev/null +++ b/src/copapy/stencils.c @@ -0,0 +1,291 @@ + + // Auto-generated stencils for copapy + // Do not edit manually + + volatile int dummy_int = 1337; + volatile float dummy_float = 1337; + + void result_int(int arg1); + + void result_float(float arg1); + + void result_int_int(int arg1, int arg2); + + void result_int_float(int arg1, float arg2); + + void result_float_int(float arg1, int arg2); + + void result_float_float(float arg1, float arg2); + + void add_int_int(int arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_int_int(arg1 + arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void add_int_float(int arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float(arg1 + arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void add_float_int(float arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_int(arg1 + arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void add_float_float(float arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float(arg1 + arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void sub_int_int(int arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_int_int(arg1 - arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void sub_int_float(int arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float(arg1 - arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void sub_float_int(float arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_int(arg1 - arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void sub_float_float(float arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float(arg1 - arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void mul_int_int(int arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_int_int(arg1 * arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void mul_int_float(int arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float(arg1 * arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void mul_float_int(float arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_int(arg1 * arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void mul_float_float(float arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float(arg1 * arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void div_int_int(int arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_int((float)arg1 / arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void div_int_float(int arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float((float)arg1 / arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void div_float_int(float arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_int(arg1 / arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void div_float_float(float arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float(arg1 / arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void gt_int_int(int arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_int_int(arg1 > arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void gt_int_float(int arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float(arg1 > arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void gt_float_int(float arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_int(arg1 > arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void gt_float_float(float arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float(arg1 > arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void eq_int_int(int arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_int_int(arg1 == arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void eq_int_float(int arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float(arg1 == arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void eq_float_int(float arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_int(arg1 == arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void eq_float_float(float arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float(arg1 == arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void mod_int_int(int arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_int_int(arg1 % arg2, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void read_int_reg0_int_int(int arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_int_int(dummy_int, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void read_int_reg1_int_int(int arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_int_int(arg1, dummy_int); + asm volatile (".long 0xE2401F0F"); + } + + void read_float_reg0_int_int(int arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_int(dummy_float, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void read_float_reg1_int_int(int arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_int_float(arg1, dummy_float); + asm volatile (".long 0xE2401F0F"); + } + + void read_int_reg0_int_float(int arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_int_float(dummy_int, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void read_int_reg1_int_float(int arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_int_int(arg1, dummy_int); + asm volatile (".long 0xE2401F0F"); + } + + void read_float_reg0_int_float(int arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float(dummy_float, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void read_float_reg1_int_float(int arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_int_float(arg1, dummy_float); + asm volatile (".long 0xE2401F0F"); + } + + void read_int_reg0_float_int(float arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_int_int(dummy_int, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void read_int_reg1_float_int(float arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_int(arg1, dummy_int); + asm volatile (".long 0xE2401F0F"); + } + + void read_float_reg0_float_int(float arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_int(dummy_float, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void read_float_reg1_float_int(float arg1, int arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float(arg1, dummy_float); + asm volatile (".long 0xE2401F0F"); + } + + void read_int_reg0_float_float(float arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_int_float(dummy_int, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void read_int_reg1_float_float(float arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_int(arg1, dummy_int); + asm volatile (".long 0xE2401F0F"); + } + + void read_float_reg0_float_float(float arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float(dummy_float, arg2); + asm volatile (".long 0xE2401F0F"); + } + + void read_float_reg1_float_float(float arg1, float arg2) { + asm volatile (".long 0xE1401F0F"); + result_float_float(arg1, dummy_float); + asm volatile (".long 0xE2401F0F"); + } + + void write_int(int arg1) { + asm volatile (".long 0xE1401F0F"); + dummy_int = arg1; + result_int(arg1); + asm volatile (".long 0xE2401F0F"); + } + + void write_float(float arg1) { + asm volatile (".long 0xE1401F0F"); + dummy_float = arg1; + result_float(arg1); + asm volatile (".long 0xE2401F0F"); + } + + int function_start(){ + result_int(0); // dummy call instruction before marker gets striped + asm volatile (".long 0xE2401F0F"); + return 1; + } + + int function_end(){ + result_int(0); + asm volatile (".long 0xE1401F0F"); + return 1; + } + \ No newline at end of file diff --git a/test.sh b/test.sh deleted file mode 100644 index 29b7fb2..0000000 --- a/test.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -set -e -echo "Compile..." -python tests/test_compile.py -echo "Run..." -echo "-----------------------------------" -bin/coparun bin/test.copapy \ No newline at end of file diff --git a/build.bat b/tools/build.bat similarity index 100% rename from build.bat rename to tools/build.bat diff --git a/build.sh b/tools/build.sh similarity index 89% rename from build.sh rename to tools/build.sh index ac9258b..6c5d3b3 100644 --- a/build.sh +++ b/tools/build.sh @@ -1,9 +1,9 @@ #!/bin/bash set -e set -v -SRC=src/copapy/stencils.c +SRC=bin/stencils.c DEST=src/copapy/obj -python src/copapy/generate_stencils.py $SRC +python tools/generate_stencils.py $SRC mkdir -p $DEST gcc -c $SRC -O0 -o $DEST/stencils_x86_64_O0.o gcc -c $SRC -O1 -o $DEST/stencils_x86_64_O1.o diff --git a/src/copapy/obj/crosscompile.sh b/tools/crosscompile.sh similarity index 88% rename from src/copapy/obj/crosscompile.sh rename to tools/crosscompile.sh index de0f4be..c65707b 100644 --- a/src/copapy/obj/crosscompile.sh +++ b/tools/crosscompile.sh @@ -13,18 +13,18 @@ set -e set -v -SRC=src/copapy/stencils.c +SRC=bin/stencils.c DEST=src/copapy/obj OPT=O3 mkdir -p $DEST # Windows x86_64 (ARM64) -python src/copapy/generate_stencils.py --abi ms $SRC +python tools/generate_stencils.py --abi ms $SRC gcc-12 -$OPT -c $SRC -o $DEST/stencils_AMD64_$OPT.o # Native x86_64 -python src/copapy/generate_stencils.py $SRC +python tools/generate_stencils.py $SRC gcc-12 -$OPT -c $SRC -o $DEST/stencils_x86_64_$OPT.o # ARM64 diff --git a/tools/extract_code.py b/tools/extract_code.py index 09d18ef..becaf8a 100644 --- a/tools/extract_code.py +++ b/tools/extract_code.py @@ -1,70 +1,76 @@ from copapy.binwrite import data_reader, Command, ByteOrder from copapy.stencil_db import RelocationType +import argparse -input_file = "bin/test.copapy" +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("input_file", type=str, help="Input file path with copapy commands") + parser.add_argument("output_file", type=str, help="Output file with patched code") + parser.add_argument("--data_section_offset", type=int, default=0x2000, help="Offset for data relative to code section") + parser.add_argument("--byteorder", type=str, choices=['little', 'big'], default='little', help="Select byteorder") + args = parser.parse_args() -output_file = "bin/test_code.bin" + input_file: str = args.input_file + output_file: str = args.output_file + data_section_offset: int = args.data_section_offset + byteorder: ByteOrder = args.byteorder -data_section_offset = 0x2000 + with open(input_file, mode='rb') as f: + dr = data_reader(f.read(), byteorder) -byteorder: ByteOrder = 'little' + buffer_index: int = 0 + end_flag: int = 0 + program_data: bytearray = bytearray([]) -with open(input_file, mode='rb') as f: - dr = data_reader(f.read(), byteorder) + while (end_flag == 0): + com = dr.read_com() -buffer_index: int = 0 -end_flag: int = 0 -program_data: bytearray = bytearray([]) + if com == Command.ALLOCATE_DATA: + size = dr.read_int() + print(f"ALLOCATE_DATA size={size}") + elif com == Command.ALLOCATE_CODE: + size = dr.read_int() + program_data = bytearray(size) + print(f"ALLOCATE_CODE size={size}") + elif com == Command.COPY_DATA: + offs = dr.read_int() + size = dr.read_int() + datab = dr.read_bytes(size) + print(f"COPY_DATA offs={offs} size={size} data={' '.join(hex(d) for d in datab)}") + elif com == Command.COPY_CODE: + offs = dr.read_int() + size = dr.read_int() + datab = dr.read_bytes(size) + program_data[offs:offs + size] = datab + print(f"COPY_CODE offs={offs} size={size} data={' '.join(hex(d) for d in datab[:5])}...") + elif com == Command.PATCH_FUNC: + offs = dr.read_int() + reloc_type = dr.read_int() + value = dr.read_int(signed=True) + print(f"PATCH_FUNC patch_offs={offs} reloc_type={reloc_type} value={value}") + elif com == Command.PATCH_OBJECT: + offs = dr.read_int() + reloc_type = dr.read_int() + value = dr.read_int(signed=True) + assert reloc_type == RelocationType.RELOC_RELATIVE_32.value + program_data[offs:offs + 4] = (value + data_section_offset).to_bytes(4, byteorder, signed=True) + print(f"PATCH_OBJECT patch_offs={offs} reloc_type={reloc_type} value={value}") + elif com == Command.RUN_PROG: + rel_entr_point = dr.read_int() + print(f"RUN_PROG rel_entr_point={rel_entr_point}") + elif com == Command.READ_DATA: + offs = dr.read_int() + size = dr.read_int() + print(f"READ_DATA offs={offs} size={size}") + elif com == Command.FREE_MEMORY: + print("READ_DATA") + elif com == Command.END_PROG: + print("END_PROG") + end_flag = 1 + else: + assert False, f"Unknown command: {com}" -while (end_flag == 0): - com = dr.read_com() + with open(output_file, mode='wb') as f: + f.write(program_data) - if com == Command.ALLOCATE_DATA: - size = dr.read_int() - print(f"ALLOCATE_DATA size={size}") - elif com == Command.ALLOCATE_CODE: - size = dr.read_int() - program_data = bytearray(size) - print(f"ALLOCATE_CODE size={size}") - elif com == Command.COPY_DATA: - offs = dr.read_int() - size = dr.read_int() - datab = dr.read_bytes(size) - print(f"COPY_DATA offs={offs} size={size} data={' '.join(hex(d) for d in datab)}") - elif com == Command.COPY_CODE: - offs = dr.read_int() - size = dr.read_int() - datab = dr.read_bytes(size) - program_data[offs:offs + size] = datab - print(f"COPY_CODE offs={offs} size={size} data={' '.join(hex(d) for d in datab[:5])}...") - elif com == Command.PATCH_FUNC: - offs = dr.read_int() - reloc_type = dr.read_int() - value = dr.read_int(signed=True) - print(f"PATCH_FUNC patch_offs={offs} reloc_type={reloc_type} value={value}") - elif com == Command.PATCH_OBJECT: - offs = dr.read_int() - reloc_type = dr.read_int() - value = dr.read_int(signed=True) - assert reloc_type == RelocationType.RELOC_RELATIVE_32.value - program_data[offs:offs + 4] = (value + data_section_offset).to_bytes(4, byteorder, signed=True) - print(f"PATCH_OBJECT patch_offs={offs} reloc_type={reloc_type} value={value}") - elif com == Command.RUN_PROG: - rel_entr_point = dr.read_int() - print(f"RUN_PROG rel_entr_point={rel_entr_point}") - elif com == Command.READ_DATA: - offs = dr.read_int() - size = dr.read_int() - print(f"READ_DATA offs={offs} size={size}") - elif com == Command.FREE_MEMORY: - print("READ_DATA") - elif com == Command.END_PROG: - print("END_PROG") - end_flag = 1 - else: - assert False, f"Unknown command: {com}" - -with open(output_file, mode='wb') as f: - f.write(program_data) - -print('OK') + print(f"Code written to {output_file}.") diff --git a/src/copapy/generate_stencils.py b/tools/generate_stencils.py similarity index 100% rename from src/copapy/generate_stencils.py rename to tools/generate_stencils.py diff --git a/tools/inspect.sh b/tools/inspect.sh new file mode 100644 index 0000000..4abceac --- /dev/null +++ b/tools/inspect.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +source tools/build.sh +python tests/test_compile_div.py +python tools/extract_code.py "bin/test.copapy" "bin/test.copapy.bin" +objdump -D -b binary -m i386:x86-64 --adjust-vma=0x1000 bin/test.copapy.bin > bin/test.copapy.asm diff --git a/src/copapy/obj/nativecompile.sh b/tools/nativecompile.sh similarity index 70% rename from src/copapy/obj/nativecompile.sh rename to tools/nativecompile.sh index ceb59c4..85d5edf 100644 --- a/src/copapy/obj/nativecompile.sh +++ b/tools/nativecompile.sh @@ -2,10 +2,10 @@ set -e set -v -SRC=src/copapy/stencils.c +SRC=bin/stencils.c DEST=src/copapy/obj -python src/copapy/generate_stencils.py $SRC +python tools/generate_stencils.py $SRC mkdir -p $DEST gcc-12 -c $SRC -O0 -o $DEST/stencils_x86_64_O0.o @@ -13,7 +13,7 @@ gcc-12 -c $SRC -O1 -o $DEST/stencils_x86_64_O1.o gcc-12 -c $SRC -O2 -o $DEST/stencils_x86_64_O2.o gcc-12 -c $SRC -O3 -o $DEST/stencils_x86_64_O3.o -python src/copapy/generate_stencils.py --abi ms $SRC +python tools/generate_stencils.py --abi ms $SRC gcc-12 -c $SRC -O0 -o $DEST/stencils_AMD64_O0.o gcc-12 -c $SRC -O1 -o $DEST/stencils_AMD64_O1.o