3 takes powerpc assembly instructions and turns them into LE/BE binary
4 data. calls powerpc64-linux-gnu-as, ld and objcopy to do so.
12 filedir
= os
.path
.dirname(os
.path
.realpath(__file__
))
13 memmap
= os
.path
.join(filedir
, "memmap")
16 endian_fmt
= "elf64-big"
21 def __init__(self
, instructions
):
22 if isinstance(instructions
, list):
23 instructions
= '\n'.join(instructions
)
24 self
.assembly
= instructions
+ '\n' # plus final newline
30 def __exit__(self
, type, value
, traceback
):
33 def _get_binary(self
, elffile
):
34 self
.binfile
= tempfile
.NamedTemporaryFile(suffix
=".bin")
35 args
= ["powerpc64-linux-gnu-objcopy",
40 subprocess
.check_output(args
)
42 def _link(self
, ofile
):
43 with tempfile
.NamedTemporaryFile(suffix
=".elf") as elffile
:
44 args
= ["powerpc64-linux-gnu-ld",
48 subprocess
.check_output(args
)
49 self
._get
_binary
(elffile
)
52 with tempfile
.NamedTemporaryFile(suffix
=".o") as outfile
:
53 args
= ["powerpc64-linux-gnu-as",
58 p
= subprocess
.Popen(args
, stdin
=subprocess
.PIPE
)
59 p
.communicate(self
.assembly
.encode('utf-8'))
63 def generate_instructions(self
):
65 data
= self
.binfile
.read(4)
68 yield struct
.unpack('<i', data
)[0]
74 curpos
= self
.binfile
.tell()
75 self
.binfile
.seek(0, 2) # Seek to end of file
76 size
= self
.binfile
.tell()
77 self
.binfile
.seek(curpos
, 0)