diff --git a/tests/Dockerfile b/tests/Dockerfile deleted file mode 100644 index c74ffef..0000000 --- a/tests/Dockerfile +++ /dev/null @@ -1,43 +0,0 @@ -# Start with a base image containing build tools and libraries -FROM debian:stable - -# Set the working directory -WORKDIR /src - -# Install necessary build tools -RUN apt-get update && apt-get install -y \ - build-essential \ - gcc -# g++ \ -# make \ -# wget \ -# curl \ -# git \ -# ca-certificates \ -# bison \ -# flex \ -# python3 \ -# texinfo - -# Install cross-compilers for x86, ARM, MIPS, and RISC-V -RUN apt-get install -y \ - gcc-12-arm-linux-gnueabihf \ - gcc-12-mips-linux-gnu \ - gcc-12-riscv64-linux-gnu \ - gcc-12-aarch64-linux-gnu - -# Copy the C file into the container -COPY test.c /src/ - -# Compile the C file for x86 -CMD gcc -c -O0 test.c -o ../obj/test-x86-o0.o && \ - gcc -c -O3 test.c -o ../obj/test-x86-o3.o && \ - # Compile the C file for ARM - arm-linux-gnueabihf-gcc-12 -c -O0 test.c -o ../obj/test-arm-o0.o && \ - arm-linux-gnueabihf-gcc-12 -c -O3 test.c -o ../obj/test-arm-o3.o && \ - # Compile the C file for MIPS - mips-linux-gnu-gcc-12 -c -O0 test.c -o ../obj/test-mips-o0.o && \ - mips-linux-gnu-gcc-12 -c -O3 test.c -o ../obj/test-mips-o3.o && \ - # Compile the C file for RISC-V - riscv64-linux-gnu-gcc-12 -c -O0 test.c -o ../obj/test-riscv-o0.o && \ - riscv64-linux-gnu-gcc-12 -c -O3 test.c -o ../obj/test-riscv-o3.o diff --git a/tests/compile_test_file.sh b/tests/compile_test_file.sh deleted file mode 100644 index a422d5e..0000000 --- a/tests/compile_test_file.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -docker build -t pelfy-test-cross-compiler . -docker run --rm -v $(pwd)/obj:/obj pelfy-test-cross-compiler \ No newline at end of file diff --git a/tests/src/Dockerfile b/tests/src/Dockerfile new file mode 100644 index 0000000..9209021 --- /dev/null +++ b/tests/src/Dockerfile @@ -0,0 +1,46 @@ +# Start with a base image containing build tools and libraries +FROM debian:stable + +# Set the working directory +WORKDIR /src + +# Install necessary build tools +RUN apt-get update && apt-get install -y \ + build-essential \ + gcc +# g++ \ +# make \ +# wget \ +# curl \ +# git \ +# ca-certificates \ +# bison \ +# flex \ +# python3 \ +# texinfo + +# Install cross-compilers for x86, ARM, MIPS, and RISC-V +RUN apt-get install -y \ + gcc-12-arm-linux-gnueabihf \ + gcc-12-mips-linux-gnu \ + gcc-12-riscv64-linux-gnu \ + gcc-12-aarch64-linux-gnu + +RUN apt-get install -y \ + gcc-12-riscv32-linux-gnu \ + gcc-12-aarch64-linux-gnu + +COPY * /src/ + +CMD bash make_objs.sh gcc -O0 && \ + bash make_objs.sh gcc -O3 && \ + bash make_objs.sh arm-linux-gnueabihf-gcc-12 -O0 && \ + bash make_objs.sh arm-linux-gnueabihf-gcc-12 -O3 && \ + bash make_objs.sh aarch64-linux-gnueabihf-gcc-12 -O0 && \ + bash make_objs.sh aarch64-linux-gnueabihf-gcc-12 -O3 && \ + bash make_objs.sh mips-linux-gnu-gcc-12 -O0 && \ + bash make_objs.sh mips-linux-gnu-gcc-12 -O3 && \ + bash make_objs.sh riscv32-linux-gnu-gcc-12 -O0 && \ + bash make_objs.sh riscv32-linux-gnu-gcc-12 -O3 && \ + bash make_objs.sh riscv64-linux-gnu-gcc-12 -O0 && \ + bash make_objs.sh riscv64-linux-gnu-gcc-12 -O3 \ No newline at end of file diff --git a/tests/src/make_objs.sh b/tests/src/make_objs.sh new file mode 100644 index 0000000..de9abd0 --- /dev/null +++ b/tests/src/make_objs.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# Check if correct number of arguments is provided +if [ $# -lt 2 ]; then + echo "Usage: $0 " + exit 1 +fi + +COMPILER=$1 +OPTIMIZATION_FLAG=$2 + +# Check if there are any .c files in the directory +if ls *.c &> /dev/null; then + for file in *.c; do + obj_file="${file%}_${COMPILER}_${OPTIMIZATION_FLAG}.o" + echo "Compiling $file -> $obj_file with $COMPILER and $OPTIMIZATION_FLAG" + $COMPILER -c "$file" $OPTIMIZATION_FLAG -o "$obj_file" + if [ $? -ne 0 ]; then + echo "Compilation failed for $file" + exit 1 + fi + done + echo "All files compiled successfully." +else + echo "No .c files found in the current directory." + exit 2 +fi diff --git a/tests/src/run_cross_compilation.sh b/tests/src/run_cross_compilation.sh new file mode 100644 index 0000000..35c03c1 --- /dev/null +++ b/tests/src/run_cross_compilation.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +docker build -t pelfy-test-cross-compiler . +docker run --rm -v $(pwd)/../obj:/obj pelfy-test-cross-compiler \ No newline at end of file diff --git a/tests/test.c b/tests/src/test.c similarity index 100% rename from tests/test.c rename to tests/src/test.c diff --git a/tests/test_example_elfs.py b/tests/test_example_elfs.py index 48fad9f..5cfb445 100644 --- a/tests/test_example_elfs.py +++ b/tests/test_example_elfs.py @@ -1,13 +1,28 @@ import pelfy +import glob +def known_name(text: str): + return not text.isnumeric() and not text.startswith('0x') def test_simple_c(): - elf = pelfy.open_elf_file('tests/obj/test3_o3.o') + for path in glob.glob('tests/obj/*.o'): + print(f'Open {path}...') + elf = pelfy.open_elf_file(path) - print(elf.sections) - print(elf.symbols) - print(elf.code_relocations) + print(elf) + print(elf.sections) + print(elf.symbols) + print(elf.code_relocations) + print('\n') + for section in elf.sections: + assert known_name(section.description), f"Section type {section.type} for {elf.architecture} in {path} is unknown." + + for sym in elf.symbols: + assert known_name(sym.info), f"Symbol info {sym.info} for {elf.architecture} in {path} is unknown." + + for reloc in elf.get_relocations(): + assert known_name(reloc.type), f"Relocation type {reloc.type} for {elf.architecture} in {path} is unknown." if __name__ == '__main__': test_simple_c()