fxconv: add an elf_multi() function to produce multiple variables

This commit is contained in:
Lephenixnoir 2023-08-15 21:59:17 +02:00
parent cf3ab5d5e0
commit 11e3b614c2
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
1 changed files with 27 additions and 10 deletions

View File

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