d77f9d7839777c3bd607d04cdfc4c617c3dc4268
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 def __init__(self
, instructions
, bigendian
):
19 self
.bigendian
= bigendian
21 self
.endian_fmt
= "elf64-big"
26 self
.endian_fmt
= "elf64-little"
29 if isinstance(instructions
, str): # filename
30 # read instructions into a BytesIO to avoid "too many open files"
31 with
open(instructions
, "rb") as f
:
33 self
.binfile
= BytesIO(b
, 'rb')
34 self
.assembly
= '' # noo disassemble number fiiive
35 print("program", self
.binfile
)
37 if isinstance(instructions
, list):
38 instructions
= '\n'.join(instructions
)
39 self
.assembly
= instructions
+ '\n' # plus final newline
41 self
._instructions
= list(self
._get
_instructions
())
46 def __exit__(self
, type, value
, traceback
):
49 def _get_binary(self
, elffile
):
50 self
.binfile
= tempfile
.NamedTemporaryFile(suffix
=".bin")
51 args
= ["powerpc64-linux-gnu-objcopy",
53 "-I", self
.endian_fmt
,
56 subprocess
.check_output(args
)
58 def _link(self
, ofile
):
59 with tempfile
.NamedTemporaryFile(suffix
=".elf") as elffile
:
60 args
= ["powerpc64-linux-gnu-ld",
65 subprocess
.check_output(args
)
66 self
._get
_binary
(elffile
)
69 with tempfile
.NamedTemporaryFile(suffix
=".o") as outfile
:
70 args
= ["powerpc64-linux-gnu-as",
75 p
= subprocess
.Popen(args
, stdin
=subprocess
.PIPE
)
76 p
.communicate(self
.assembly
.encode('utf-8'))
78 print("Error in program:")
83 def _get_instructions(self
):
85 data
= self
.binfile
.read(4)
88 yield struct
.unpack('<I', data
)[0] # unsigned int
90 def generate_instructions(self
):
91 yield from self
._instructions
97 curpos
= self
.binfile
.tell()
98 self
.binfile
.seek(0, 2) # Seek to end of file
99 size
= self
.binfile
.tell()
100 self
.binfile
.seek(curpos
, 0)