Skip to content


LiefAddSegmentConfig (ComponentConfig) dataclass

Config for the LiefAddSegmentModifier.


Name Type Description
virtual_address int

virtual address of the new segment

alignment int

alignment of the new segment

content List[int]

list of integers representing the raw bytes of the new segment

rwx_flags str

string representation of the new segment's R/W/X permissions

replace_note bool

replace the unused NOTE segment with the new segment, rather than adding a new segment. defaults to True, as adding a new segment may corrupt the ELF due to a LIEF bug.

physical_address Optional[int]

overwrite the default physical address (defaults to the virtual address)

LiefAddSegmentModifier (Modifier)

modify(self, resource, config) async

Modify the given resource.

Users should not call this method directly; rather, they should run


Name Type Description Default
resource Resource required
config LiefAddSegmentConfig

Optional config for modification. If an implementation provides a default, this default will always be used when config would otherwise be None. Note that a copy of the default config will be passed, so the default config values cannot be modified persistently by a component run.

Source code in ofrak/core/elf/
async def modify(self, resource: Resource, config: LiefAddSegmentConfig) -> None:
    binary: lief.ELF.Binary = lief.parse(await resource.get_data())

    segment = lief.ELF.Segment()
    segment.type = lief.ELF.SEGMENT_TYPES.LOAD
    segment.content = config.content
    segment.alignment = config.alignment
    segment.virtual_address = config.virtual_address
    if config.physical_address is not None:
        segment.physical_address = config.physical_address
    if "r" in config.rwx_flags:
    if "w" in config.rwx_flags:
    if "x" in config.rwx_flags:

    if config.replace_note:
        # instead of adding a segment to the binary, replace a useless NOTE segment
        #   see
        #   and
        if not binary.has(lief.ELF.SEGMENT_TYPES.NOTE):
            raise ValueError("Binary must have a NOTE section to add a new section")
        segment = binary.replace(segment, binary[lief.ELF.SEGMENT_TYPES.NOTE])
        if config.physical_address is not None:
            segment.physical_address = config.physical_address
        _ = binary.add(segment)

    with tempfile.NamedTemporaryFile() as temp_file:
        with open(, "rb") as f_handle:
            new_data =
    # replace all old content (old range) with new content from Lief
    resource.queue_patch(Range(0, await resource.get_data_length()), new_data)