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.
13 filedir
= os
.path
.dirname(os
.path
.realpath(__file__
))
14 memmap
= os
.path
.join(filedir
, "memmap")
17 endian_fmt
= "elf64-big"
22 def __init__(self
, instructions
):
23 if isinstance(instructions
, str): # filename
24 self
.binfile
= open(instructions
, "rb")
25 self
.assembly
= '' # noo disassemble number fiiive
26 print ("program", self
.binfile
)
28 if isinstance(instructions
, list):
29 instructions
= '\n'.join(instructions
)
30 self
.assembly
= instructions
+ '\n' # plus final newline
32 self
._instructions
= list(self
._get
_instructions
())
37 def __exit__(self
, type, value
, traceback
):
40 def _get_binary(self
, elffile
):
41 self
.binfile
= tempfile
.NamedTemporaryFile(suffix
=".bin")
42 args
= ["powerpc64-linux-gnu-objcopy",
47 subprocess
.check_output(args
)
49 def _link(self
, ofile
):
50 with tempfile
.NamedTemporaryFile(suffix
=".elf") as elffile
:
51 args
= ["powerpc64-linux-gnu-ld",
55 subprocess
.check_output(args
)
56 self
._get
_binary
(elffile
)
59 with tempfile
.NamedTemporaryFile(suffix
=".o") as outfile
:
60 args
= ["powerpc64-linux-gnu-as",
65 p
= subprocess
.Popen(args
, stdin
=subprocess
.PIPE
)
66 p
.communicate(self
.assembly
.encode('utf-8'))
68 print("Error in program:")
73 def _get_instructions(self
):
75 data
= self
.binfile
.read(4)
78 yield struct
.unpack('<i', data
)[0]
80 def generate_instructions(self
):
81 yield from self
._instructions
87 curpos
= self
.binfile
.tell()
88 self
.binfile
.seek(0, 2) # Seek to end of file
89 size
= self
.binfile
.tell()
90 self
.binfile
.seek(curpos
, 0)