about summary refs log tree commit diff stats
path: root/example/jitter/run_with_linuxenv.py
diff options
context:
space:
mode:
authorFabrice Desclaux <fabrice.desclaux@cea.fr>2019-02-25 11:09:54 +0100
committerFabrice Desclaux <fabrice.desclaux@cea.fr>2019-03-05 16:52:49 +0100
commit02bbb30efea4980c9d133947cbbf69fb599071ad (patch)
tree3fea6826fcc5354840a27cb1dc99ff31eef81896 /example/jitter/run_with_linuxenv.py
parenteab809932871f91d6f4aa770fc321af9e156e0f5 (diff)
downloadmiasm-02bbb30efea4980c9d133947cbbf69fb599071ad.tar.gz
miasm-02bbb30efea4980c9d133947cbbf69fb599071ad.zip
Support python2/python3
Diffstat (limited to 'example/jitter/run_with_linuxenv.py')
-rw-r--r--example/jitter/run_with_linuxenv.py43
1 files changed, 26 insertions, 17 deletions
diff --git a/example/jitter/run_with_linuxenv.py b/example/jitter/run_with_linuxenv.py
index f4900a96..fda76f9a 100644
--- a/example/jitter/run_with_linuxenv.py
+++ b/example/jitter/run_with_linuxenv.py
@@ -24,8 +24,8 @@ if args.verbose:
     syscall.log.setLevel(logging.DEBUG)
 
 # Get corresponding interpreter and reloc address
-cont_target_tmp = Container.from_stream(open(args.target))
-ld_path = str(cont_target_tmp.executable.getsectionbyname(".interp").content).strip("\x00")
+cont_target_tmp = Container.from_stream(open(args.target, 'rb'))
+ld_path = bytes(cont_target_tmp.executable.getsectionbyname(".interp").content).strip(b"\x00")
 if cont_target_tmp.executable.Ehdr.type in [elf_csts.ET_REL, elf_csts.ET_DYN]:
     elf_base_addr = 0x40000000
 elif cont_target_tmp.executable.Ehdr.type == elf_csts.ET_EXEC:
@@ -52,29 +52,38 @@ else:
 
 # Load the interpreter in memory, applying relocation
 linux_env = LinuxEnvironment()
-linux_env.filesystem.passthrough.append(re.compile(args.passthrough))
+linux_env.filesystem.passthrough.append(re.compile(args.passthrough.encode()))
 ld_path = linux_env.filesystem.resolve_path(ld_path)
-cont_ld = Container.from_stream(open(ld_path),
-                                vm=jitter.vm,
-                                addr=0x80000000,
-                                apply_reloc=True)
+cont_ld = Container.from_stream(
+    open(ld_path, "rb"),
+    vm=jitter.vm,
+    addr=0x80000000,
+    apply_reloc=True
+)
 # Load the target ELF in memory, without applying reloc
 loc_db = cont_ld.loc_db
-cont_target = Container.from_stream(open(args.target), vm=jitter.vm,
-                                    loc_db=loc_db,
-                                    addr=elf_base_addr,
-                                    apply_reloc=False)
+cont_target = Container.from_stream(
+    open(args.target, "rb"),
+    vm=jitter.vm,
+    loc_db=loc_db,
+    addr=elf_base_addr,
+    apply_reloc=False
+)
 # PHDR containing the PH header
-elf_phdr_header = [ph32.ph for ph32 in cont_target.executable.ph
-                   if ph32.ph.type == elf_csts.PT_PHDR][0]
+elf_phdr_header = next(
+    ph32.ph for ph32 in cont_target.executable.ph
+    if ph32.ph.type == elf_csts.PT_PHDR
+)
 
 # Prepare the desired environment
-argv = [args.target] + args.extra_args
+argv = [args.target.encode()] + [arg.encode() for arg in args.extra_args]
 if args.flags:
     argv += ["-%s" % args.flags]
-envp = {"PATH": "/usr/local/bin", "USER": linux_env.user_name}
-auxv = environment.AuxVec(elf_base_addr + elf_phdr_header.vaddr,
-                          cont_target.entry_point, linux_env)
+envp = {b"PATH": b"/usr/local/bin", b"USER": linux_env.user_name}
+auxv = environment.AuxVec(
+    elf_base_addr + elf_phdr_header.vaddr,
+    cont_target.entry_point, linux_env
+)
 prepare_loader(jitter, argv, envp, auxv, linux_env)
 syscall.enable_syscall_handling(jitter, linux_env, syscall_callbacks)