6 filedir
= os
.path
.dirname(os
.path
.realpath(__file__
))
7 memmap
= os
.path
.join(filedir
, "memmap")
10 endian_fmt
= "elf64-big"
15 def __init__(self
, instructions
):
16 if isinstance(instructions
, list):
17 instructions
= '\n'.join(instructions
)
18 self
.assembly
= instructions
+ '\n' # plus final newline
24 def __exit__(self
, type, value
, traceback
):
27 def _get_binary(self
, elffile
):
28 self
.binfile
= tempfile
.NamedTemporaryFile(suffix
=".bin")
29 args
= ["powerpc64-linux-gnu-objcopy",
34 subprocess
.check_output(args
)
36 def _link(self
, ofile
):
37 with tempfile
.NamedTemporaryFile(suffix
=".elf") as elffile
:
38 args
= ["powerpc64-linux-gnu-ld",
42 subprocess
.check_output(args
)
43 self
._get
_binary
(elffile
)
46 with tempfile
.NamedTemporaryFile(suffix
=".o") as outfile
:
47 args
= ["powerpc64-linux-gnu-as",
51 p
= subprocess
.Popen(args
, stdin
=subprocess
.PIPE
)
52 p
.communicate(self
.assembly
.encode('utf-8'))
56 def generate_instructions(self
):
58 data
= self
.binfile
.read(4)
61 yield struct
.unpack('<i', data
)[0]
67 curpos
= self
.binfile
.tell()
68 self
.binfile
.seek(0, 2) # Seek to end of file
69 size
= self
.binfile
.tell()
70 self
.binfile
.seek(curpos
, 0)