Docker cross compilation scripts for test generation updated

This commit is contained in:
Nicolas Kruse 2025-02-27 16:54:20 +01:00
parent ea2a3ebca9
commit f06f68681b
7 changed files with 96 additions and 51 deletions

View File

@ -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

View File

@ -1,4 +0,0 @@
#!/bin/sh
docker build -t pelfy-test-cross-compiler .
docker run --rm -v $(pwd)/obj:/obj pelfy-test-cross-compiler

46
tests/src/Dockerfile Normal file
View File

@ -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

27
tests/src/make_objs.sh Normal file
View File

@ -0,0 +1,27 @@
#!/bin/bash
# Check if correct number of arguments is provided
if [ $# -lt 2 ]; then
echo "Usage: $0 <compiler> <optimization_flag>"
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

View File

@ -0,0 +1,4 @@
#!/bin/sh
docker build -t pelfy-test-cross-compiler .
docker run --rm -v $(pwd)/../obj:/obj pelfy-test-cross-compiler

View File

@ -1,13 +1,28 @@
import pelfy import pelfy
import glob
def known_name(text: str):
return not text.isnumeric() and not text.startswith('0x')
def test_simple_c(): 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)
print(elf.sections) print(elf.sections)
print(elf.symbols) print(elf.symbols)
print(elf.code_relocations) 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__': if __name__ == '__main__':
test_simple_c() test_simple_c()