From d8b3085bdd04006749e95339a4998d3e63132125 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Thu, 30 Nov 2023 23:55:41 -0800 Subject: [PATCH] add utilities for testing ELF files --- src/openpower/test/elf/__init__.py | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/openpower/test/elf/__init__.py 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() -- 2.30.2