[docs]classSymbolType(Enum):""" ABI-agnostic symbol types """TYPE_OTHER=0TYPE_NONE=1TYPE_FUNCTION=2TYPE_OBJECT=3TYPE_SECTION=4TYPE_TLS_OBJECT=5
[docs]classSymbolSubType(Enum):""" Abstract base class for ABI-specific symbol types """
[docs]defto_base_type(self)->SymbolType:# pylint: disable=no-self-use""" A subclass' ABI-specific mapping to :SymbolType: """raiseValueError("Abstract base class SymbolSubType has no base_type")
[docs]classSymbol:""" Representation of a symbol from a binary file. Smart enough to rebase itself. There should never be more than one Symbol instance representing a single symbol. To make sure of this, only use the :meth:`cle.backends.Backend.get_symbol()` to create new symbols. :ivar owner: The object that contains this symbol :vartype owner: cle.backends.Backend :ivar str name: The name of this symbol :ivar int addr: The un-based address of this symbol, an RVA :ivar int size: The size of this symbol :ivar _type: The ABI-agnostic type of this symbol :ivar bool resolved: Whether this import symbol has been resolved to a real symbol :ivar resolvedby: The real symbol this import symbol has been resolve to :vartype resolvedby: None or cle.backends.Symbol :ivar str resolvewith: The name of the library we must use to resolve this symbol, or None if none is required. """
[docs]def__init__(self,owner:Backend,name:str,relative_addr:int,size:int,sym_type:SymbolType):""" Not documenting this since if you try calling it, you're wrong. """self.owner:Backend=ownerself.name=nameself.relative_addr=relative_addrself.size=sizeself._type:SymbolType=SymbolType(sym_type)self.resolved=Falseself.resolvedby=None
def__repr__(self):ifself.is_import:returnf'<Symbol "{self.name}" in {self.owner.binary_basename} (import)>'else:returnf'<Symbol "{self.name}" in {self.owner.binary_basename} at {self.rebased_addr:#x}>'
@propertydeftype(self)->SymbolType:""" The ABI-agnostic SymbolType. Must be overridden by derived types. """returnself._type@propertydefsubtype(self)->SymbolSubType:""" A subclass' ABI-specific types """raiseValueError("Base class Symbol has no subtype")@propertydefrebased_addr(self):""" The address of this symbol in the global memory space """returnAT.from_rva(self.relative_addr,self.owner).to_mva()@propertydeflinked_addr(self):returnAT.from_rva(self.relative_addr,self.owner).to_lva()@propertydefis_function(self):""" Whether this symbol is a function """returnself.typeisSymbolType.TYPE_FUNCTION# These may be overridden in subclassesis_static=Falseis_common=Falseis_import=Falseis_export=Falseis_local=Falseis_weak=Falseis_extern=Falseis_forward=False
[docs]defresolve_forwarder(self):""" If this symbol is a forwarding export, return the symbol the forwarding refers to, or None if it cannot be found """returnself
# compatibility layer_complained_owner=False@propertydefowner_obj(self):ifnotSymbol._complained_owner:Symbol._complained_owner=Truelog.critical("Deprecation warning: use symbol.owner instead of symbol.owner_obj")returnself.ownerdef__getstate__(self):return{k:vfork,vinself.__dict__.items()ifk!="owner"}def__setstate__(self,state):self.__dict__.update(state)