Class SegmentedAddressSpace

java.lang.Object
ghidra.program.model.address.GenericAddressSpace
ghidra.program.model.address.SegmentedAddressSpace
All Implemented Interfaces:
AddressSpace, Comparable<AddressSpace>
Direct Known Subclasses:
ProtectedAddressSpace

public class SegmentedAddressSpace extends GenericAddressSpace
Address Space for dealing with (intel) segmented address spaces. It understands the mapping between the segmented encoding (seg:offset) and the flat address encoding necessary to produce an Address object that can be used by other analyses. This mapping is inherent in protected methods: - getDefaultOffsetFromFlat - getDefaultSegmentFromFlat - getFlatOffset - getOffsetFromFlat - getAddressInSegment These 5 methods can be overridden to get a different mapping. This base class is set up to map as for x86 16-bit real-mode.
  • Field Details

    • minOffset

      protected final long minOffset
    • maxOffset

      protected long maxOffset
    • minAddress

      protected final Address minAddress
    • maxAddress

      protected Address maxAddress
    • spaceSize

      protected long spaceSize
    • spaceID

      protected final int spaceID
  • Constructor Details

    • SegmentedAddressSpace

      protected SegmentedAddressSpace(String name, int size, int unique)
      Constructor for larger size address spaces (than the real-mode space)
      Parameters:
      name - is the name of the space
      size - is the number of bits in a (flat) address
      unique - is the unique id for the space
    • SegmentedAddressSpace

      public SegmentedAddressSpace(String name, int unique)
      Constructs a new Segmented AddressSpace for x86 real-mode, with 21-bit addresses.
      Parameters:
      name - is the name of the space
      unique - is the unique id for the space.
  • Method Details

    • getFlatOffset

      protected long getFlatOffset(int segment, long offset)
      Given a 16-bit segment and an offset, produce the flat address offset
      Parameters:
      segment - is the segment value
      offset - is the 16-bit offset into the segment
      Returns:
      the encoded flat offset
    • getDefaultSegmentFromFlat

      protected int getDefaultSegmentFromFlat(long flat)
      Given a flat address offset, extract the default 16-bit segment portion
      Parameters:
      flat - is the flat offset
      Returns:
      the segment value
    • getDefaultOffsetFromFlat

      protected long getDefaultOffsetFromFlat(long flat)
      Given a flat address offset, extract the offset portion assuming the default segment.
      Parameters:
      flat - is the flat offset
      Returns:
      the offset value
    • getOffsetFromFlat

      protected long getOffsetFromFlat(long flat, int segment)
      Given a flat address offset, extract a segment offset assuming a specific segment value.
      Parameters:
      flat - is the flat offset
      segment - is the specific segment value
      Returns:
      the segment offset
    • getAddressInSegment

      protected SegmentedAddress getAddressInSegment(long flat, int preferredSegment)
      Given a flat address offset and a preferred segment, try to create an address that maps to the offset and is in the segment. For architectures like x86 real-mode, multiple address encodings can map to the same flat address offset. This method tries to select between the different encodings. If the flat offset cannot be encoded with the preferred segment, null is returned.
      Parameters:
      flat - is the flat offset
      preferredSegment - is the 16-bit preferred segment value
      Returns:
      the segment encoded address or null
    • getAddress

      public Address getAddress(String addrString, boolean caseSensitive) throws AddressFormatException
      Description copied from interface: AddressSpace
      Parses the String into an address within this address space.
      Specified by:
      getAddress in interface AddressSpace
      Parameters:
      addrString - the string to parse as an address.
      caseSensitive - specifies if addressSpace names must match case.
      Returns:
      an address if the string parsed successfully or null if the AddressSpace specified in the addrString is not this space.
      Throws:
      AddressFormatException - if the string cannot be parsed or the parsed offset is larger than the size for this space.
    • subtract

      public Address subtract(Address addr, long displacement)
      Description copied from interface: AddressSpace
      Creates a new address (possibly in a new space) by subtracting the given displacement from the given address.
      Specified by:
      subtract in interface AddressSpace
      Parameters:
      addr - original address being subtracted from
      displacement - amount to subtract
      Returns:
      the new address
      See Also:
    • add

      public Address add(Address addr, long displacement)
      Description copied from interface: AddressSpace
      Creates a new address (possibly in a new space) by adding the given displacement from the given address.
      Specified by:
      add in interface AddressSpace
      Parameters:
      addr - original address being subtracted from
      displacement - amount to subtract
      Returns:
      the new address
      See Also:
    • getAddress

      public SegmentedAddress getAddress(long byteOffset)
      Description copied from interface: AddressSpace
      Returns a new address in this space with the given byte offset. NOTE: This method is the same as invoking getAddress(long byteOffset, false).
      Specified by:
      getAddress in interface AddressSpace
      Overrides:
      getAddress in class GenericAddressSpace
      Parameters:
      byteOffset - the byte offset for the new address.
      Returns:
      address with given byte offset
      See Also:
    • getAddressInThisSpaceOnly

      public SegmentedAddress getAddressInThisSpaceOnly(long byteOffset)
      Description copied from interface: AddressSpace
      Get a byte address from this address space. Don't allow overlay spaces to remap the address into a base space when the address is not contained in the bounds of the overlay region.
      Specified by:
      getAddressInThisSpaceOnly in interface AddressSpace
      Overrides:
      getAddressInThisSpaceOnly in class GenericAddressSpace
      Parameters:
      byteOffset - the byte offset for the new address.
      Returns:
      an address if the offset is valid.
      See Also:
    • getUncheckedAddress

      protected SegmentedAddress getUncheckedAddress(long byteOffset)
      Instantiates an address within this space. No offset validation should be performed.
      Overrides:
      getUncheckedAddress in class GenericAddressSpace
      Parameters:
      byteOffset - address offset
      Returns:
      requested unchecked address
      See Also:
      • AbstractAddressSpace.getUncheckedAddress(long)
    • getAddress

      public SegmentedAddress getAddress(int segment, int segmentOffset)
      Generates a segmented address with the given segment, offset, and overlay id.
      Parameters:
      segment - the segment
      segmentOffset - the offset in the segment
      Returns:
      SegmentedAddress the newly created segmented address.
    • getNextOpenSegment

      public int getNextOpenSegment(Address addr)
      Get the segment index for the first segment whose start address comes after the given address
      Parameters:
      addr - is the given address
      Returns:
      the segment index
    • getPhysicalSpace

      public SegmentedAddressSpace getPhysicalSpace()
      Description copied from interface: AddressSpace
      Returns the physical space associated with an address space. There is always exactly one physical space associated with an address space (it may be its own physical space).
      Specified by:
      getPhysicalSpace in interface AddressSpace
      Returns:
      the associated physical space.
      See Also:
    • getPointerSize

      public int getPointerSize()
      Description copied from interface: AddressSpace
      Returns the absolute size of a pointer into this space (in bytes).
      Specified by:
      getPointerSize in interface AddressSpace
      See Also:
    • hasSignedOffset

      public boolean hasSignedOffset()
      Description copied from interface: AddressSpace
      Returns true if space uses signed offset
      Specified by:
      hasSignedOffset in interface AddressSpace
    • getSize

      public int getSize()
      Description copied from interface: AddressSpace
      Returns the number of bits that are used to form the address. Thus the maximum offset for this address space will be 2^size-1.
      Specified by:
      getSize in interface AddressSpace
    • getAddressableUnitSize

      public int getAddressableUnitSize()
      Description copied from interface: AddressSpace
      Returns the number of data bytes which correspond to each addressable location within this space (i.e., word-size in bytes). NOTE: When transforming a byte-offset to an addressable word offset the method AddressSpace.getAddressableWordOffset(long) should be used instead of simple division. When transforming an addressable word-offset to a byte-offset simple multiplication may be used. Neither of these transformations perform address space bounds checking.
         byteOffset = wordOffset * addressUnitSize
         wordOffset = getAddressableWordOffset(byteOffset)
       
      Specified by:
      getAddressableUnitSize in interface AddressSpace
    • getAddressableWordOffset

      public long getAddressableWordOffset(long byteOffset)
      Description copied from interface: AddressSpace
      Get the addressable memory word offset which corresponds to the specified memory byte offset. This method handles some of the issues of unsigned math when stuck using Java's signed long primitives. No space bounds checking is performed.
      Specified by:
      getAddressableWordOffset in interface AddressSpace
      Parameters:
      byteOffset - memory byte offset
      Returns:
      addressable memory word offset
    • getType

      public int getType()
      Description copied from interface: AddressSpace
      Returns the type of this address space
      Specified by:
      getType in interface AddressSpace
    • getUnique

      public int getUnique()
      Returns the unique id value for this space.
      Specified by:
      getUnique in interface AddressSpace
    • getAddress

      public final Address getAddress(String addrString) throws AddressFormatException
      Description copied from interface: AddressSpace
      Parses the String into an address within this address space.
      Specified by:
      getAddress in interface AddressSpace
      Parameters:
      addrString - the string to parse as an address.
      Returns:
      an address if the string parsed successfully or null if the AddressSpace specified in the addrString is not this space.
      Throws:
      AddressFormatException - if the string cannot be parsed or the parsed offset is larger than the size for this space.
    • getAddress

      public Address getAddress(long offset, boolean isAddressableWordOffset) throws AddressOutOfBoundsException
      Description copied from interface: AddressSpace
      Returns a new address in this space with the given offset. NOTE: for those spaces with an addressable unit size other than 1, the address returned may not correspond to an addressable unit/word boundary if a byte-offset is specified.
      Specified by:
      getAddress in interface AddressSpace
      Parameters:
      offset - the offset for the new address.
      isAddressableWordOffset - if true the specified offset is an addressable unit/word offset, otherwise offset is a byte offset. See AddressSpace.getAddressableUnitSize() to understand the distinction (i.e., wordOffset = byteOffset * addressableUnitSize).
      Returns:
      address with given offset
      Throws:
      AddressOutOfBoundsException - if the offset is less than 0 or greater than the max offset allowed for this space.
    • getTruncatedAddress

      public Address getTruncatedAddress(long offset, boolean isAddressableWordOffset)
      Description copied from interface: AddressSpace
      Returns a new address in this space with the given offset. The specified offset will be truncated within the space and will not throw an exception. NOTE: for those spaces with an addressable unit size other than 1, the address returned may not correspond to a word boundary (addressable unit) if a byte-offset is specified.
      Specified by:
      getTruncatedAddress in interface AddressSpace
      Parameters:
      offset - the offset for the new address.
      isAddressableWordOffset - if true the specified offset is an addressable unit/word offset, otherwise offset is a byte offset. See AddressSpace.getAddressableUnitSize() to understand the distinction (i.e., wordOffset = byteOffset * addressableUnitSize).
      Returns:
      address with given byte offset truncated to the physical space size
    • subtract

      public long subtract(Address addr1, Address addr2)
      Description copied from interface: AddressSpace
      Calculates the displacement between addr1 and addr2 (addr1 - addr2)
      Specified by:
      subtract in interface AddressSpace
      Parameters:
      addr1 - the address to subtract from.
      addr2 - the address to subtract.
      Returns:
      the difference. (addr1.offset - addr2.offset).
    • subtractWrap

      public Address subtractWrap(Address addr, long displacement)
      Description copied from interface: AddressSpace
      Creates a new address by subtracting displacement from addr's offset.
      Specified by:
      subtractWrap in interface AddressSpace
      Parameters:
      addr - the original address. The new address will wrap in a manner that depends on the address space. For a generic address space this will wrap at the extents of the address space. For a segmented address space it will wrap at the extents of the segment.
      displacement - the displacement to subtract.
      Returns:
      a new address created by subtracting the displacement from addr.offset.
    • subtractWrapSpace

      public Address subtractWrapSpace(Address addr, long displacement)
      Description copied from interface: AddressSpace
      Creates a new address by subtracting the displacement from the given address. If the offset is greater than the max offset of the address space, the high order bits are masked off, making the address wrap. For non-segmented addresses this will be the same as subtractWrap(). For segmented addresses, the address will wrap when the 20 bit (oxfffff) offset is exceeded, as opposed to when the segment offset is exceeded.
      Specified by:
      subtractWrapSpace in interface AddressSpace
      Parameters:
      addr - the address to subtract the displacement from.
      displacement - the displacement to subtract.
      Returns:
      The new Address formed by subtracting the displacement from the specified address.
    • subtractNoWrap

      public Address subtractNoWrap(Address addr, long displacement) throws AddressOverflowException
      Description copied from interface: AddressSpace
      Creates a new address by subtracting displacement from addr's offset. The new offset will NOT wrap!
      Specified by:
      subtractNoWrap in interface AddressSpace
      Parameters:
      addr - the original address
      displacement - the displacement to subtract.
      Returns:
      The new address created by subtracting displacement from addr.offset.
      Throws:
      AddressOverflowException - if the subtraction would cause a wrap,
    • addWrap

      public Address addWrap(Address addr, long displacement)
      Description copied from interface: AddressSpace
      Creates a new address by adding displacement to the given address. The resulting address may wrap. The new address will wrap in a manner that depends on the address space. For a generic address space this will wrap at the extents of the address space. For a segmented address space it will wrap at the extents of the segment.
      Specified by:
      addWrap in interface AddressSpace
      Parameters:
      addr - the original address.
      displacement - the displacement to add.
      Returns:
      the new address created by adding displacement to addr.offset.
    • addWrapSpace

      public Address addWrapSpace(Address addr, long displacement)
      Description copied from interface: AddressSpace
      Creates a new address by adding the displacement to the given address. If the offset is greater than the max offset of the address space, the high order bits are masked off, making the address wrap. For non-segmented addresses this will be the same as addWrap(). For segmented addresses, the address will wrap when the 20 bit (oxfffff) offset is exceeded, as opposed to when the segment offset is exceeded.
      Specified by:
      addWrapSpace in interface AddressSpace
      Parameters:
      addr - the address to add the displacement to.
      displacement - the displacement to add.
      Returns:
      The new Address formed by adding the displacement to the specified addresst.
    • addNoWrap

      public Address addNoWrap(Address addr, long displacement) throws AddressOverflowException
      Description copied from interface: AddressSpace
      Creates a new address by adding displacement to the given address. The new address will NOT wrap!
      Specified by:
      addNoWrap in interface AddressSpace
      Parameters:
      addr - the original address.
      displacement - the displacement to add.
      Returns:
      The new address created by adding displacement to addr.offset.
      Throws:
      AddressOverflowException - if the addition would cause a wrap,
    • addNoWrap

      public Address addNoWrap(GenericAddress addr, BigInteger displacement) throws AddressOverflowException
      Description copied from interface: AddressSpace
      Creates a new address by adding displacement to the given address. The new address will NOT wrap!
      Specified by:
      addNoWrap in interface AddressSpace
      Parameters:
      addr - the original address.
      displacement - the displacement to add.
      Returns:
      The new address created by adding displacement to addr.offset.
      Throws:
      AddressOverflowException - if the addition would cause a wrap,
    • isValidRange

      public boolean isValidRange(long byteOffset, long length)
      Description copied from interface: AddressSpace
      Check the specified address range for validity within this space. Segmented spaces will restrict a range to a single segment.
      Specified by:
      isValidRange in interface AddressSpace
      Parameters:
      byteOffset -
      length -
      Returns:
      true if range is valid for this space
    • isSuccessor

      public boolean isSuccessor(Address addr1, Address addr2)
      Description copied from interface: AddressSpace
      Tests whether addr2 immediately follows addr1.
      Specified by:
      isSuccessor in interface AddressSpace
      Parameters:
      addr1 - the first address.
      addr2 - the second address.
    • getMaxAddress

      public Address getMaxAddress()
      Description copied from interface: AddressSpace
      Get the maximum address allowed for this AddressSpace. NOTE: Use of this method to identify the region associated with an overlay memory block within its overlay address space is no longer supported. Defined regions of an overlay space may now be determined using OverlayAddressSpace.getOverlayAddressSet().
      Specified by:
      getMaxAddress in interface AddressSpace
      Returns:
      maximum address of this address space.
    • getMinAddress

      public Address getMinAddress()
      Description copied from interface: AddressSpace
      Get the minimum address allowed for this AddressSpace. For a memory space the returned address will have an offset of 0 within this address space. NOTE: Use of this method to identify the region associated with an overlay memory block within its overlay address space is no longer supported. Defined regions of an overlay space may now be determined using OverlayAddressSpace.getOverlayAddressSet().
      Specified by:
      getMinAddress in interface AddressSpace
      Returns:
      minimum address of this address space.
    • compareTo

      public int compareTo(AddressSpace space)
      Specified by:
      compareTo in interface Comparable<AddressSpace>
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public final int hashCode()
      Overrides:
      hashCode in class Object
    • getSpaceID

      public int getSpaceID()
      Description copied from interface: AddressSpace
      Get the ID for this space
      Specified by:
      getSpaceID in interface AddressSpace
      Returns:
      space ID
    • testAddressSpace

      protected void testAddressSpace(Address addr)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • showSpaceName

      public boolean showSpaceName()
      Description copied from interface: AddressSpace
      Returns true if the address should display its addressSpace name.
      Specified by:
      showSpaceName in interface AddressSpace
    • getOverlayAddress

      public Address getOverlayAddress(Address addr)
      No overlay translation necessary, this is a base addressSpace. (non-Javadoc)
      Specified by:
      getOverlayAddress in interface AddressSpace
      Parameters:
      addr - address possibly falling within this overlay space.
      Returns:
      an address relative to this overlay
      See Also:
    • makeValidOffset

      public long makeValidOffset(long offset) throws AddressOutOfBoundsException
      Description copied from interface: AddressSpace
      Tests if the offset if valid. If the space is signed, then it sign extends the offset.
      Specified by:
      makeValidOffset in interface AddressSpace
      Parameters:
      offset - the offset to test and/or sign extend
      Returns:
      the valid positive offset or appropriate sign extended offset.
      Throws:
      AddressOutOfBoundsException - if offset is invalid
    • truncateOffset

      public long truncateOffset(long offset)
      Description copied from interface: AddressSpace
      Truncate the specified byte offset within this space to produce a valid offset.
      Specified by:
      truncateOffset in interface AddressSpace
      Parameters:
      offset - any byte offset
      Returns:
      truncated byte offset
    • truncateAddressableWordOffset

      public long truncateAddressableWordOffset(long wordOffset)
      Description copied from interface: AddressSpace
      Truncate the specified addressable unit/word offset within this space to produce a valid offset.
      Specified by:
      truncateAddressableWordOffset in interface AddressSpace
      Parameters:
      wordOffset - any addressable unit/word offset
      Returns:
      truncated word offset
    • isMemorySpace

      public boolean isMemorySpace()
      Description copied from interface: AddressSpace
      Returns true if this space represents a memory address. NOTE: It is important to make the distinction between Loaded and Non-Loaded memory addresses. Program importers may create memory blocks associated with Non-Loaded file content which are not associated with processor defined memory regions. While Loaded file content is placed into memory blocks which are associated with specific memory address spaces defined by the processor language specification.
      Specified by:
      isMemorySpace in interface AddressSpace
      See Also:
    • isLoadedMemorySpace

      public boolean isLoadedMemorySpace()
      Description copied from interface: AddressSpace
      Returns true if this space represents represents a Loaded Memory region (e.g., processor RAM).
      Specified by:
      isLoadedMemorySpace in interface AddressSpace
    • isNonLoadedMemorySpace

      public boolean isNonLoadedMemorySpace()
      Description copied from interface: AddressSpace
      Returns true if this space represents represents a Non-Loaded storage region for retaining non-loaded file data (e.g., OTHER)
      Specified by:
      isNonLoadedMemorySpace in interface AddressSpace
    • isHashSpace

      public boolean isHashSpace()
      Description copied from interface: AddressSpace
      Returns true if this space represents a location in the HASH space.
      Specified by:
      isHashSpace in interface AddressSpace
    • isRegisterSpace

      public boolean isRegisterSpace()
      Description copied from interface: AddressSpace
      Returns true if this space represents a register location
      Specified by:
      isRegisterSpace in interface AddressSpace
    • isStackSpace

      public boolean isStackSpace()
      Description copied from interface: AddressSpace
      Returns true if this space represents a stack location
      Specified by:
      isStackSpace in interface AddressSpace
    • isUniqueSpace

      public boolean isUniqueSpace()
      Description copied from interface: AddressSpace
      Returns true if this space in the unique space
      Specified by:
      isUniqueSpace in interface AddressSpace
    • isConstantSpace

      public boolean isConstantSpace()
      Description copied from interface: AddressSpace
      Returns true if this space in the constant space
      Specified by:
      isConstantSpace in interface AddressSpace
    • isVariableSpace

      public boolean isVariableSpace()
      Description copied from interface: AddressSpace
      Returns true if this space represents a variable location
      Specified by:
      isVariableSpace in interface AddressSpace
    • isExternalSpace

      public boolean isExternalSpace()
      Description copied from interface: AddressSpace
      Returns true if this space in the EXTERNAL_SPACE
      Specified by:
      isExternalSpace in interface AddressSpace
    • isOverlaySpace

      public boolean isOverlaySpace()
      Description copied from interface: AddressSpace
      Returns true if this addressSpace is an OverlayAddressSpace
      Specified by:
      isOverlaySpace in interface AddressSpace
    • setShowSpaceName

      public void setShowSpaceName(boolean b)
    • hasMappedRegisters

      public boolean hasMappedRegisters()
      Description copied from interface: AddressSpace
      Returns true if this space has registers that are mapped into it. This means that registers could actually have pointers to them.
      Specified by:
      hasMappedRegisters in interface AddressSpace
      Returns:
      true if this space has any registers mapped in it.
    • setHasMappedRegisters

      public void setHasMappedRegisters(boolean hasRegisters)
      Tag this memory space as having memory mapped registers
      Parameters:
      hasRegisters - true if it has registers, false otherwise