[docs]classRegion:""" A region of memory that is mapped in the object's file. :ivar offset: The offset into the file the region starts. :ivar vaddr: The virtual address. :ivar filesize: The size of the region in the file. :ivar memsize: The size of the region when loaded into memory. The prefix `v-` on a variable or parameter name indicates that it refers to the virtual, loaded memory space, while a corresponding variable without the `v-` refers to the flat zero-based memory of the file. When used next to each other, `addr` and `offset` refer to virtual memory address and file offset, respectively. """vaddr:intmemsize:intfilesize:int
def_rebase(self,delta):""" Does region rebasing to other base address. Intended for usage by loader's add_object to reflect the rebasing. :param int delta: Delta offset between an old and a new image bases """self.vaddr+=delta
[docs]defcontains_addr(self,addr):""" Does this region contain this virtual address? """returnself.vaddr<=addr<self.vaddr+self.memsize
[docs]defcontains_offset(self,offset):""" Does this region contain this offset into the file? """returnself.offset<=offset<self.offset+self.filesize
[docs]defaddr_to_offset(self,addr):""" Convert a virtual memory address into a file offset """offset=addr-self.vaddr+self.offsetifnotself.contains_offset(offset):returnNonereturnoffset
[docs]defoffset_to_addr(self,offset):""" Convert a file offset into a virtual memory address """addr=offset-self.offset+self.vaddrifnotself.contains_addr(addr):returnNonereturnaddr
def__repr__(self):return"<{}{}>".format(self.__class__.__name__,", ".join([f"{k}={hex(v)ifvisnotNoneelse'None'}"fork,vinself.__dict__.items()]),)@propertydefmax_addr(self):""" The maximum virtual address of this region """returnself.vaddr+self.memsize-1@propertydefmin_addr(self):""" The minimum virtual address of this region """returnself.vaddr@propertydefmax_offset(self):""" The maximum file offset of this region """returnself.offset+self.filesize-1
[docs]defmin_offset(self):""" The minimum file offset of this region """returnself.offset
# EDG says: Blobs now have segments, and SimOS will get upset if these don't exist. See simos.py line 107 for# some code you should probably fix if you don't like it.@propertydefis_readable(self)->bool:# pylint: disable=no-self-usereturnTrue@propertydefis_writable(self)->bool:# pylint: disable=no-self-usereturnTrue@propertydefis_executable(self)->bool:# pylint: disable=no-self-usereturnTrue
@propertydefis_executable(self):returnself._is_executable@propertydefis_writable(self):returnself._is_writable@propertydefis_readable(self):returnself._is_readable@propertydefonly_contains_uninitialized_data(self):""" Whether this section is initialized to zero after the executable is loaded. """returnTrue
[docs]classSection(Region):""" Simple representation of a loaded section. :ivar str name: The name of the section """
[docs]def__init__(self,name,offset,vaddr,size):""" :param str name: The name of the section :param int offset: The offset into the binary file this section begins :param int vaddr: The address in virtual memory this section begins :param int size: How large this section is """super().__init__(offset,vaddr,size,size)self.name=name
@propertydefis_readable(self):""" Whether this section has read permissions """raiseNotImplementedError()@propertydefis_writable(self):""" Whether this section has write permissions """raiseNotImplementedError()@propertydefis_executable(self):""" Whether this section has execute permissions """raiseNotImplementedError()@propertydefonly_contains_uninitialized_data(self):""" Whether this section is initialized to zero after the executable is loaded. """raiseNotImplementedError()def__repr__(self):return"<{} | offset {:#x}, vaddr {:#x}, size {:#x}>".format(self.nameifself.nameelse"Unnamed",self.offset,self.vaddr,self.memsize)