Segments
segments
Classes:
-
AddSegmentFlags
– -
AddressingMode
– -
PredefinedClass
– -
SegmentPermissions
– -
Segments
–Provides access to segment-related operations in the IDA database.
AddSegmentFlags
Bases: IntFlag
Attributes:
FILLGAP
class-attribute
instance-attribute
FILLGAP = ADDSEG_FILLGAP
IDBENC
class-attribute
instance-attribute
IDBENC = ADDSEG_IDBENC
NOAA
class-attribute
instance-attribute
NOAA = ADDSEG_NOAA
NONE
class-attribute
instance-attribute
NONE = 0
NOSREG
class-attribute
instance-attribute
NOSREG = ADDSEG_NOSREG
NOTRUNC
class-attribute
instance-attribute
NOTRUNC = ADDSEG_NOTRUNC
OR_DIE
class-attribute
instance-attribute
OR_DIE = ADDSEG_OR_DIE
QUIET
class-attribute
instance-attribute
QUIET = ADDSEG_QUIET
SPARSE
class-attribute
instance-attribute
SPARSE = ADDSEG_SPARSE
AddressingMode
PredefinedClass
Bases: Enum
Attributes:
ABS
class-attribute
instance-attribute
ABS = 'ABS'
BSS
class-attribute
instance-attribute
BSS = 'BSS'
CODE
class-attribute
instance-attribute
CODE = 'CODE'
COMM
class-attribute
instance-attribute
COMM = 'COMM'
CONST
class-attribute
instance-attribute
CONST = 'CONST'
DATA
class-attribute
instance-attribute
DATA = 'DATA'
STACK
class-attribute
instance-attribute
STACK = 'STACK'
XTRN
class-attribute
instance-attribute
XTRN = 'XTRN'
SegmentPermissions
Bases: IntFlag
Attributes:
ALL
class-attribute
instance-attribute
ALL = SEGPERM_MAXVAL
EXEC
class-attribute
instance-attribute
EXEC = SEGPERM_EXEC
NONE
class-attribute
instance-attribute
NONE = 0
READ
class-attribute
instance-attribute
READ = SEGPERM_READ
WRITE
class-attribute
instance-attribute
WRITE = SEGPERM_WRITE
Segments
Segments(database: Database)
Bases: DatabaseEntity
Provides access to segment-related operations in the IDA database.
Can be used to iterate over all segments in the opened database.
Args: database: Reference to the active IDA database.
Note: Since this class does not manage the lifetime of IDA kernel objects (segment_t*), it is recommended to use these pointers within a limited scope. Obtain the pointer, perform the necessary operations, and avoid retaining references beyond the immediate context to prevent potential issues with object invalidation.
Methods:
-
add
–Adds a new segment to the IDA database.
-
add_permissions
–OR the given permission bits into the existing segment permissions.
-
append
–Append a new segment directly after the last segment in the database.
-
get_all
–Retrieves an iterator over all segments in the database.
-
get_at
–Retrieves the segment that contains the given address.
-
get_bitness
–Get segment bitness (16/32/64).
-
get_by_name
–Find segment by name.
-
get_class
–Get segment class name.
-
get_comment
–Get comment for segment.
-
get_name
–Retrieves the name of the given segment.
-
get_size
–Calculate segment size in bytes.
-
remove_permissions
–Clear the given permission bits from the existing segment permissions.
-
set_addressing_mode
–Sets the segment addressing mode (16-bit, 32-bit, or 64-bit).
-
set_comment
–Set comment for segment.
-
set_name
–Renames a segment.
-
set_permissions
–Set the segment permissions exactly to
perms
(overwrites existing flags).
Attributes:
-
database
(Database
) –Get the database reference, guaranteed to be non-None when called from
-
m_database
–
database
property
database: Database
Get the database reference, guaranteed to be non-None when called from methods decorated with @check_db_open.
Returns: The active database instance.
Note: This property should only be used in methods decorated with @check_db_open, which ensures m_database is not None.
m_database
instance-attribute
m_database = database
add
add(
seg_para: ea_t,
start_ea: ea_t,
end_ea: ea_t,
seg_name: Optional[str] = None,
seg_class: Optional[Union[str, PredefinedClass]] = None,
flags: AddSegmentFlags = NONE,
) -> Optional[segment_t]
Adds a new segment to the IDA database.
Args: seg_para: Segment base paragraph. start_ea: Start address of the segment (linear EA). end_ea: End address of the segment (exclusive). seg_name: Name of new segment (optional). seg_class: Class of the segment (optional). Accepts str or PredefinedClass. flags: Add segment flags (AddSegmentFlags).
Returns: The created segment_t on success, or None on failure.
add_permissions
add_permissions(
segment: segment_t, perms: SegmentPermissions
) -> bool
OR the given permission bits into the existing segment permissions.
append
append(
seg_para: ea_t,
seg_size: ea_t,
seg_name: Optional[str] = None,
seg_class: Optional[Union[str, PredefinedClass]] = None,
flags: AddSegmentFlags = NONE,
) -> Optional[segment_t]
Append a new segment directly after the last segment in the database.
Args: seg_para: Segment base paragraph (selector/paragraph as used by IDA). seg_size: Desired size in bytes for the new segment (must be > 0). seg_name: Optional name for the new segment. seg_class: Optional class for the new segment (str or PredefinedClass). flags: Add segment flags (AddSegmentFlags).
Returns: The created segment_t on success, or None on failure.
Raises: ValueError: If seg_size is <= 0. RuntimeError: If there are no existing segments to append after.
get_all
get_all() -> Iterator[segment_t]
Retrieves an iterator over all segments in the database.
Returns: A generator yielding all segment_t objects in the database.
get_at
get_at(ea: ea_t) -> Optional[segment_t]
Retrieves the segment that contains the given address.
Args: ea: The effective address to search.
Returns: A segment_t object, or None if none found.
Raises: InvalidEAError: If the effective address is invalid.
get_bitness
get_bitness(segment: segment_t) -> int
Get segment bitness (16/32/64).
get_by_name
get_by_name(name: str) -> Optional[segment_t]
Find segment by name.
Args: name: Segment name to search for
Returns: segment_t if found, None otherwise
get_class
get_class(segment: segment_t) -> Optional[str]
Get segment class name.
get_comment
get_comment(
segment: segment_t, repeatable: bool = False
) -> str
Get comment for segment.
Args: segment: The segment to get comment from. repeatable: If True, retrieves repeatable comment (shows at all identical operands). If False, retrieves non-repeatable comment (shows only at this segment).
Returns: Comment text, or empty string if no comment exists.
get_name
get_name(segment: segment_t) -> str
Retrieves the name of the given segment.
Args: segment: The segment to get the name from.
Returns: The segment name as a string, or an empty string if unavailable.
get_size
get_size(segment: segment_t) -> int
Calculate segment size in bytes.
remove_permissions
remove_permissions(
segment: segment_t, perms: SegmentPermissions
) -> bool
Clear the given permission bits from the existing segment permissions.
set_addressing_mode
set_addressing_mode(
segment: segment_t, mode: AddressingMode
) -> bool
Sets the segment addressing mode (16-bit, 32-bit, or 64-bit).
Args: segment: The target segment object. mode: AddressingMode enum value.
Returns: True if successful, False otherwise.
set_comment
set_comment(
segment: segment_t,
comment: str,
repeatable: bool = False,
) -> bool
Set comment for segment.
Args: segment: The segment to set comment for. comment: Comment text to set. repeatable: If True, creates a repeatable comment (shows at all identical operands). If False, creates a non-repeatable comment (shows only at this segment).
Returns: True if successful, False otherwise.
set_name
set_name(segment: segment_t, name: str) -> bool
Renames a segment.
Args: segment: The segment to rename. name: The new name to assign to the segment.
Returns: True if the rename operation succeeded, False otherwise.
set_permissions
set_permissions(
segment: segment_t, perms: SegmentPermissions
) -> bool
Set the segment permissions exactly to perms
(overwrites existing flags).