From 11e3b614c2471bb6c60bb8c531d4108e617a0a6f Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Tue, 15 Aug 2023 21:59:17 +0200 Subject: [PATCH] fxconv: add an elf_multi() function to produce multiple variables --- fxconv/fxconv.py | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/fxconv/fxconv.py b/fxconv/fxconv.py index cb7d586..8fc4bec 100644 --- a/fxconv/fxconv.py +++ b/fxconv/fxconv.py @@ -1174,6 +1174,14 @@ def elf(data, output, symbol, toolchain=None, arch=None, section=None, Produces an output file and returns nothing. """ + # Unfold ObjectData into data and assembly + if isinstance(data, ObjectData): + assembly = f".global {symbol}\n" + \ + f"{symbol}:\n" + \ + data.link(symbol)[0] + \ + (assembly or "") + data = None + # Toolchain parameters if toolchain is None: @@ -1194,16 +1202,9 @@ def elf(data, output, symbol, toolchain=None, arch=None, section=None, raise FxconvError(f"non-trivial architecture for {toolchain} must be "+ "specified") - # Unfold ObjectData into data and assembly - if isinstance(data, ObjectData): - asm = ".section " + section.split(",",1)[0] + "\n" - asm += f".global {symbol}\n" - asm += f"{symbol}:\n" - asm += data.link(symbol)[0] - asm += (assembly or "") - - data = None - assembly = asm + if assembly: + sec = ".section " + section.split(",",1)[0] + assembly = sec + "\n" + assembly if data is None and assembly is None: raise FxconvError("elf() but no data and no assembly") @@ -1263,6 +1264,22 @@ def elf(data, output, symbol, toolchain=None, arch=None, section=None, if assembly: fp_asm.close() +def elf_multi(vars, output, assembly=None, **kwargs): + """ + Like elf(), but instead of one symbol/data pair, allows defining multiple + variables. vars should be a list [(symbol, ObjectData), ...]. Keyword + arguments are passed to elf(). + """ + + asm = "" + for symbol, objdata in vars: + asm += f".global {symbol}\n" + asm += f"{symbol}:\n" + asm += objdata.link(symbol)[0] + asm += assembly or "" + + return elf(None, output, None, assembly=asm, **kwargs) + # # Meta API #