From: Jacob Lifshay Date: Fri, 1 Dec 2023 07:55:41 +0000 (-0800) Subject: add utilities for testing ELF files X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=5b91e9f2e0c40a524bb5dd3f9adbcf856ea72d1e;p=openpower-isa.git add utilities for testing ELF files --- diff --git a/src/openpower/test/elf/__init__.py b/src/openpower/test/elf/__init__.py new file mode 100644 index 00000000..e99ff2dd --- /dev/null +++ b/src/openpower/test/elf/__init__.py @@ -0,0 +1,36 @@ +# SPDX-License-Identifier: LGPLv3+ +# Copyright (C) 2023 Jacob Lifshay +# Funded by NLnet http://nlnet.nl +""" ELF test utilities + +related bugs: + +* https://bugs.libre-soc.org/show_bug.cgi?id=1169 +""" + +from subprocess import run, PIPE +from tempfile import NamedTemporaryFile +from elftools.elf.elffile import ELFFile +from openpower.util import log, LogType + +DEF_CC_ARGS = '-Os', '-ffreestanding', '-nostdlib', '-static', '-xc' +DEF_CC = 'powerpc64le-linux-gnu-gcc' + +def compile_elf(src_code, compiler_args=DEF_CC_ARGS, compiler=DEF_CC): + if isinstance(compiler, str): + compiler = [compiler] + f = NamedTemporaryFile(suffix=".elf") + args = [*compiler, *compiler_args, '-', '-o', f.name] + cleanup = f.close + try: + run(args, input=src_code, check=True, encoding='utf-8') + dump_out = run([ + 'powerpc64le-linux-gnu-objdump', '-dfprsF', '-Mraw', f.name], + stdout=PIPE, check=True, encoding='utf-8').stdout + log(dump_out, kind=LogType.InstrInOuts) + f = ELFFile(f) + cleanup = None + return f + finally: + if cleanup is not None: + cleanup()