diff --git a/miasm/jitter/loader/pe.py b/miasm/jitter/loader/pe.py index 9bd488773..02558e6c3 100644 --- a/miasm/jitter/loader/pe.py +++ b/miasm/jitter/loader/pe.py @@ -323,8 +323,12 @@ def vm2pe(myjit, fname, libs=None, e_orig=None, addrs = list(all_mem) addrs.sort() entry_point = mye.virt2rva(myjit.pc) - if not 0 < entry_point < 0xFFFFFFFF: - raise ValueError("Cannot compute a valid entry point RVA") + if entry_point is None or not 0 < entry_point < 0xFFFFFFFF: + raise ValueError( + "Current pc (0x%x) used as entry point seems to be out of the binary" % + myjit.pc + ) + mye.Opthdr.AddressOfEntryPoint = entry_point first = True for ad in addrs: diff --git a/miasm/loader/pe_init.py b/miasm/loader/pe_init.py index 74192849e..f5baa9a51 100644 --- a/miasm/loader/pe_init.py +++ b/miasm/loader/pe_init.py @@ -476,18 +476,30 @@ def off2rva(self, off): return return off - section.offset + section.addr - def virt2rva(self, virt): - if virt is None: - return - return virt - self.NThdr.ImageBase + def virt2rva(self, addr): + """ + Return rva of virtual address @addr; None if addr is below ImageBase + """ + if addr is None: + return None + rva = addr - self.NThdr.ImageBase + if rva < 0: + return None + return rva def rva2virt(self, rva): if rva is None: return return rva + self.NThdr.ImageBase - def virt2off(self, virt): - return self.rva2off(self.virt2rva(virt)) + def virt2off(self, addr): + """ + Return offset of virtual address @addr + """ + rva = self.virt2rva(addr) + if rva is None: + return None + return self.rva2off(rva) def off2virt(self, off): return self.rva2virt(self.off2rva(off))