# Path: cle/backends/pe/relocation/pereloc.py
# class PEReloc(Relocation):
#     AUTO_HANDLE_NONE = True
# 
#     def __init__(self, owner, symbol, addr, resolvewith=None):   # pylint: disable=unused-argument
#         super().__init__(owner, symbol, addr)
# 
#         self.resolvewith = resolvewith
#         if self.resolvewith is not None:
#             self.resolvewith = self.resolvewith.lower()
# 
#     def resolve_symbol(self, solist, bypass_compatibility=False, extern_object=None, **kwargs):
#         if not bypass_compatibility:
#             solist = [x for x in solist if self.resolvewith == x.provides]
#         super().resolve_symbol(solist, bypass_compatibility=bypass_compatibility, extern_object=extern_object, **kwargs)
# 
#         if self.resolvedby is None:
#             return
# 
#         # handle symbol forwarders
#         newsym = self.resolvedby.resolve_forwarder()
#         if newsym is None:
#             new_symbol = extern_object.make_extern(self.symbol.name, sym_type=self.symbol.type)
#             self.resolvedby.resolvedby = new_symbol
#             self.resolve(new_symbol)
#             return
# 
#         self.resolvedby = newsym
#         self.symbol.resolvedby = newsym
# 
#     def relocate(self):
#         if self.symbol is None:  # relocation described in the DIRECTORY_ENTRY_BASERELOC table
#             value = self.value
#             if value is None:
#                 l.debug('Unresolved relocation with no symbol.')
#                 return
#             self.owner.memory.store(self.relative_addr, value)
#         else:
#             super().relocate()
# 
#     @property
#     def value(self):
#         if self.resolved:
#             return self.resolvedby.rebased_addr
# 
#     @property
#     def is_base_reloc(self):
#         """
#         These relocations are ignored by the linker if the executable
#         is loaded at its preferred base address. There is no associated
#         symbol with base relocations.
#         """
#         return True if self.symbol is None else False
# 
#     @property
#     def is_import(self):
#         return not self.is_base_reloc
# 
# Path: cle/address_translator.py
# AT = AddressTranslator


import struct
import logging
from .pereloc import PEReloc
from ....address_translator import AT
        adjusted_bytes = struct.pack('<I', adjusted_value)
        return adjusted_bytes

class IMAGE_REL_BASED_HIGHLOW(PEReloc):
    @property
    def value(self):
        org_bytes = self.owner.memory.load(self.relative_addr, 4)
        org_value = struct.unpack('<I', org_bytes)[0]
        rebased_value = AT.from_lva(org_value, self.owner).to_mva()
        rebased_bytes = struct.pack('<I', rebased_value & 0xffffffff)
        return rebased_bytes

class IMAGE_REL_BASED_DIR64(PEReloc):
    @property
    def value(self):
        org_bytes = self.owner.memory.load(self.relative_addr, 8)
        org_value = struct.unpack('<Q', org_bytes)[0]
        rebased_value = AT.from_lva(org_value, self.owner).to_mva()
        if rebased_value < 0 or rebased_value >= 0x10000000000000000:
            l.error("Incorrect rebased address %x found at %s. Maybe the relocation table is broken.",
                    rebased_value, self.owner)
            return None
        rebased_bytes = struct.pack('<Q', rebased_value)
        return rebased_bytes

class IMAGE_REL_BASED_HIGH(PEReloc):
    @property
    def value(self):
        org_bytes = self.owner.memory.load(self.relative_addr, 2)
        org_value = struct.unpack('<H', org_bytes)[0]

# Path: cle/backends/pe/relocation/pereloc.py
# class PEReloc(Relocation):
#     AUTO_HANDLE_NONE = True
# 
#     def __init__(self, owner, symbol, addr, resolvewith=None):   # pylint: disable=unused-argument
#         super().__init__(owner, symbol, addr)
# 
#         self.resolvewith = resolvewith
#         if self.resolvewith is not None:
#             self.resolvewith = self.resolvewith.lower()
# 
#     def resolve_symbol(self, solist, bypass_compatibility=False, extern_object=None, **kwargs):
#         if not bypass_compatibility:
#             solist = [x for x in solist if self.resolvewith == x.provides]
#         super().resolve_symbol(solist, bypass_compatibility=bypass_compatibility, extern_object=extern_object, **kwargs)
# 
#         if self.resolvedby is None:
#             return
# 
#         # handle symbol forwarders
#         newsym = self.resolvedby.resolve_forwarder()
#         if newsym is None:
#             new_symbol = extern_object.make_extern(self.symbol.name, sym_type=self.symbol.type)
#             self.resolvedby.resolvedby = new_symbol
#             self.resolve(new_symbol)
#             return
# 
#         self.resolvedby = newsym
#         self.symbol.resolvedby = newsym
# 
#     def relocate(self):
#         if self.symbol is None:  # relocation described in the DIRECTORY_ENTRY_BASERELOC table
#             value = self.value
#             if value is None:
#                 l.debug('Unresolved relocation with no symbol.')
#                 return
#             self.owner.memory.store(self.relative_addr, value)
#         else:
#             super().relocate()
# 
#     @property
#     def value(self):
#         if self.resolved:
#             return self.resolvedby.rebased_addr
# 
#     @property
#     def is_base_reloc(self):
#         """
#         These relocations are ignored by the linker if the executable
#         is loaded at its preferred base address. There is no associated
#         symbol with base relocations.
#         """
#         return True if self.symbol is None else False
# 
#     @property
#     def is_import(self):
#         return not self.is_base_reloc
# 
# Path: cle/address_translator.py
# AT = AddressTranslator


# Path: cle/backends/pe/relocation/generic.py
import struct
import logging
from .pereloc import PEReloc
from ....address_translator import AT

        adjusted_bytes = struct.pack('<I', adjusted_value)
        return adjusted_bytes

class IMAGE_REL_BASED_HIGHLOW(PEReloc):
    @property
    def value(self):
        org_bytes = self.owner.memory.load(self.relative_addr, 4)
        org_value = struct.unpack('<I', org_bytes)[0]
        rebased_value = AT.from_lva(org_value, self.owner).to_mva()
        rebased_bytes = struct.pack('<I', rebased_value & 0xffffffff)
        return rebased_bytes

class IMAGE_REL_BASED_DIR64(PEReloc):
    @property
    def value(self):
        org_bytes = self.owner.memory.load(self.relative_addr, 8)
        org_value = struct.unpack('<Q', org_bytes)[0]
        rebased_value = AT.from_lva(org_value, self.owner).to_mva()
        if rebased_value < 0 or rebased_value >= 0x10000000000000000:
            l.error("Incorrect rebased address %x found at %s. Maybe the relocation table is broken.",
                    rebased_value, self.owner)
            return None
        rebased_bytes = struct.pack('<Q', rebased_value)
        return rebased_bytes

class IMAGE_REL_BASED_HIGH(PEReloc):
    @property
    def value(self):
        org_bytes = self.owner.memory.load(self.relative_addr, 2)
        org_value = struct.unpack('<H', org_bytes)[0]

        rebased_value = AT.from_lva(org_value, self.owner).to_mva()