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")
18 endian_fmt
= "elf64-big"
23 endian_fmt
= "elf64-little"
28 def __init__(self
, instructions
):
29 if isinstance(instructions
, str): # filename
30 self
.binfile
= open(instructions
, "rb")
31 self
.assembly
= '' # noo disassemble number fiiive
32 print ("program", self
.binfile
)
34 if isinstance(instructions
, list):
35 instructions
= '\n'.join(instructions
)
36 self
.assembly
= instructions
+ '\n' # plus final newline
38 self
._instructions
= list(self
._get
_instructions
())
43 def __exit__(self
, type, value
, traceback
):
46 def _get_binary(self
, elffile
):
47 self
.binfile
= tempfile
.NamedTemporaryFile(suffix
=".bin")
48 args
= ["powerpc64-linux-gnu-objcopy",
53 subprocess
.check_output(args
)
55 def _link(self
, ofile
):
56 with tempfile
.NamedTemporaryFile(suffix
=".elf") as elffile
:
57 args
= ["powerpc64-linux-gnu-ld",
62 subprocess
.check_output(args
)
63 self
._get
_binary
(elffile
)
66 with tempfile
.NamedTemporaryFile(suffix
=".o") as outfile
:
67 args
= ["powerpc64-linux-gnu-as",
72 p
= subprocess
.Popen(args
, stdin
=subprocess
.PIPE
)
73 p
.communicate(self
.assembly
.encode('utf-8'))
75 print("Error in program:")
80 def _get_instructions(self
):
82 data
= self
.binfile
.read(4)
85 yield struct
.unpack('<i', data
)[0]
87 def generate_instructions(self
):
88 yield from self
._instructions
94 curpos
= self
.binfile
.tell()
95 self
.binfile
.seek(0, 2) # Seek to end of file
96 size
= self
.binfile
.tell()
97 self
.binfile
.seek(curpos
, 0)