3.1.5. segment – Segment module

This module provides a number of tools that can be used to enumerate or work with segments within a database.

The base argument type for some of the utilities within this module is the idaapi.segment_t. This type is interchangeable with the address or the segment name and so either can be used to identify a segment.

When listing or enumerating segments there are different types that one can use in order to filter or match them. These types are as follows:

  • name - Match according to the exact segment name
  • like - Filter the segment names according to a glob
  • regex - Filter the function names according to a regular-expression
  • index - Match the segment by its index
  • identifier - Match the segment by its identifier
  • selector - Match the segment by its selector
  • greater or ge - Filter the segments for any after the specified address (inclusive)
  • gt - Filter the segments for any after the specified address (exclusive)
  • less or le - Filter the segments for any before the specified address (inclusive)
  • lt - Filter the segments for any before the specified address (exclusive)
  • predicate - Filter the segments by passing their idaapi.segment_t to a callable

Some examples of using these keywords are as follows:

> for l, r in database.segments(): ...
> database.segments.list(regex=r'\\.r?data')
> iterable = database.segments.iterate(like='*text*')
> result = database.segments.search(greater=0x401000)

3.1.5.1. Function list

The functions that are available in this module use multicased functions and aliases. For more information on this, please see Aliases and Multicased functions.

segment.bottom()

Return the bottom address of the current segment.

segment.bottom(segment)

Return the bottom address of the segment identified by segment.

Parameters:segment – an identifier used to specify a segment
segment.bounds()

Return the bounds of the current segment.

Aliases: segment.range

segment.bounds(segment)

Return the bounds of the segment specified by segment.

Aliases: segment.range

Parameters:segment – an identifier used to specify a segment
segment.by(segment)

Return a segment by its idaapi.segment_t.

Parameters:segment (idaapi.segment_t) – an idaapi.segment_t to return
segment.by(name)

Return the segment by its name.

Parameters:name (str) – the name of the segment to return
segment.by(ea)

Return the segment containing the address ea.

Parameters:ea (int or long) – an address belonging to the segment to return
segment.by()

Return the current segment.

segment.by(**type)

Return the segment matching the specified keywords in type.

Parameters:**type – any keyword that can be used to match the segment with
segment.by_address(ea)

Return the segment that contains the specified ea.

Aliases: segment.byaddress

Parameters:ea – an address belonging to the segment to return
segment.by_name(name)

Return the segment with the given name.

Aliases: segment.byname

Parameters:name – the name of the segment to return
segment.by_offset(offset)

Return the specified offset translated to the beginning of the current segment.

Aliases: segment.byoffset

Parameters:offset (int or long) – the offset into the current segment to return as an address
segment.by_offset(segment, offset)

Return the specified offset translated to the beginning of segment.

Aliases: segment.byoffset

Parameters:
  • segment – an identifier used to specify a segment
  • offset (int or long) – the offset into the segment to return as an address
segment.by_selector(selector)

Return the segment associated with selector.

Aliases: segment.byselector

Parameters:selector – the selector belonging to the segment to return
segment.color()

Return the color of the current segment.

segment.color(segment)

Return the color of the segment identified by segment.

Parameters:segment – an identifier used to specify a segment
segment.color(none)

Clear the color of the current segment.

Parameters:none (None) – the python value None
segment.color(segment, none)

Clear the color of the segment identified by segment.

Parameters:
  • segment – an identifier used to specify a segment
  • none (None) – the python value None
segment.color(segment, rgb)

Sets the color of the segment identified by segment to rgb.

Parameters:
  • segment – an identifier used to specify a segment
  • rgb (int or long) – the color as a red, green, and blue integer (0x00RRGGBB)
segment.contains(ea)

Returns true if the address ea is contained within the current segment.

Parameters:ea (int or long) – an address in the database
segment.contains(address, ea)

Returns true if the address ea is contained within the segment belonging to the specified address.

Parameters:
  • address (int or long) – an address belonging to a segment
  • ea (int or long) – an address in the database
segment.contains(name, ea)

Returns true if the address ea is contained within the segment with the specified name.

Parameters:
  • name (str) – the name of a segment
  • ea (int or long) – an address in the database
segment.contains(segment, ea)

Returns true if the address ea is contained within the idaapi.segment_t specified by segment.

Parameters:
  • segment (idaapi.segment_t) – an idaapi.segment_t to check
  • ea (int or long) – an address in the database
segment.go_offset(offset)

Go to the offset of the current segment.

Aliases: segment.goof, segment. gooffset, segment.gotooffset

Parameters:offset (int or long) – an offset into the current segment
segment.go_offset(segment, offset)

Go to the offset of the specified segment.

Aliases: segment.goof, segment. gooffset, segment.gotooffset

Parameters:
  • segment – an identifier used to specify a segment
  • offset (int or long) – an offset into the segment
segment.iterate()

Iterate through all of the addresses within the current segment.

segment.iterate(segment)

Iterate through all of the addresses within the specified segment.

Parameters:segment – an identifier used to specify a segment
segment.iterate(segment)

Iterate through all of the addresses within the idaapi.segment_t represented by segment.

Parameters:segment (idaapi.segment_t) – an idaapi.segment_t to iterate through
segment.list(string)

List all of the segments whose name matches the glob specified by string.

Parameters:string (str) – the glob to filter the segment names with
segment.list(**type)

List all of the segments in the database that match the keyword specified by type.

Parameters:**type – any keyword that can be used to filter the segments with
segment.load(filename, ea, size=None, offset=0, **kwds)

Load the specified filename to the address ea as a segment.

If size is not specified, use the length of the file. The keyword offset represents the offset into the file to use. The keyword name can be used to name the segment.

Parameters:
  • filename – a filename to read from
  • ea – the address to map to
  • size – the number of bytes to map
  • offset – the offset into the file to read from
  • **kwds – if name is specified as a string, then use it as the name for the new segment
segment.map(ea, size, newea, **kwds)

Map size bytes of data from ea into a new segment at newea.

The keyword name can be used to name the segment.

Parameters:
  • ea – the address of the data to map
  • size – the number of bytes to map
  • newea – the target address to map the data to
  • **kwds – if name is specified as a string, then use it as the name for the new segment
segment.name()

Return the name of the current segment.

segment.name(segment)

Return the name of the segment identified by segment.

Parameters:segment – an identifier used to specify a segment
segment.new(offset, size, name, **kwds)

Create a segment at offset with size and name it according to name.

The keyword bits can be used to specify the bit size of the segment The keyword comb can be used to specify any flags (idaapi.sc*) The keyword align can be used to specify paragraph alignment (idaapi.sa*) The keyword org specifies the origin of the segment (must be paragraph aligned due to ida)

Aliases: segment.create

Parameters:
  • offset – the offset to create the segment at
  • size – the size of the segment
  • name – the name of the segment
  • **kwds – If bits is specified, then specify the bit size of the segment. align can be used to specify the paragraph alignment. org can be used to set the origin and comb can be used to specify any other flags
segment.offset()

Return the offset of the current address from the beginning of the current segment.

segment.offset(ea)

Return the offset of the address ea from the beginning of the current segment.

Parameters:ea (int or long) – an address within the current segment
segment.offset(segment, ea)

Return the offset of the address ea from the beginning of segment.

Parameters:
  • segment – an identifier used to specify a segment
  • ea (int or long) – an address within the segment
segment.read()

Return the contents of the current segment.

Aliases: segment.string

segment.read(segment)

Return the contents of the segment identified by segment.

Aliases: segment.string

Parameters:segment – an identifier used to specify a segment
segment.remove(segment, contents=False)

Remove the specified segment.

If the bool contents is specified, then remove the contents of the segment from the database.

Aliases: segment.delete

Parameters:
  • segment – an identifier used to specify a segment
  • contents – whether to remove the contents of the segment from the database
segment.repr()

Return the current segment in a printable form.

segment.repr(segment)

Return the specified segment in a printable form.

Parameters:segment – an identifier used to specify a segment
segment.save(filename, segment, offset=0)

Export the segment identified by segment to the file named filename.

If the int offset is specified, then begin writing into the file at the specified offset.

Aliases: segment.export

Parameters:
  • filename – the path to the filename to write the segment to
  • segment – an identifier used to specify a segment
  • offset – the offset into the file to start writing the segment
segment.search(name)

Search through all the segments and return the first one matching the glob name.

Parameters:name (str) – the glob to match the segment name with
segment.search(**type)

Search through all the segments and return the first one that matches the keyword specified by type.

Parameters:**type – any keyword that can be used to filter segments with
segment.size()

Return the size of the current segment.

segment.size(segment)

Return the size of the segment specified by segment.

Parameters:segment – an identifier used to specify a segment
segment.top()

Return the top address of the current segment.

segment.top(segment)

Return the top address of the segment identified by segment.

Parameters:segment – an identifier used to specify a segment
segment.type()

Return the type of the current segment.

segment.type(ea)

Return the type of the segment containing the address ea.

Parameters:ea (int or long) –
segment.type(name)

Return the type of the segment with the specified name.

Parameters:name (str) – the name of the segment to return the type for
segment.type(segment)

Return the type of the idaapi.segment_t specified by segment.

Parameters:segment (idaapi.segment_t) – an idaapi.segment_t to return the type for
segment.type(segment, segtype)

Return whether the given segment is of the provided segtype.

Parameters:
  • segment – an idaapi.segment_t to check the type for
  • segtype (int or long) – an integer containing the idaapi.SEG_* value to check the type against
segment.within()

Returns true if the current address is within any segment.

segment.within(ea)

Returns true if the address ea is within any segment.

Parameters:ea (int or long) – an address in the database