Skip to content

elf ¤

This file declares types and methods useful for enumerating all of the address spaces and permissions of an ELF file in memory.

This is necessary for when access to /proc is restricted, or when working on a BSD system which simply does not have /proc.

Classes:

  • ELFInfo

    ELF metadata and structures.

Functions:

Attributes:

module module-attribute ¤

module = modules[__name__]

Ehdr module-attribute ¤

Ehdr = Union[Elf32_Ehdr, Elf64_Ehdr]

Phdr module-attribute ¤

Phdr = Union[Elf32_Phdr, Elf64_Phdr]

T module-attribute ¤

T = TypeVar('T', Union[Elf32_Ehdr, Elf64_Ehdr], Union[Elf32_Phdr, Elf64_Phdr])

ehdr_type_loaded module-attribute ¤

ehdr_type_loaded = 0

ELFInfo ¤

Bases: NamedTuple

ELF metadata and structures.

Attributes:

header instance-attribute ¤

header: dict[str, int | str]

sections instance-attribute ¤

sections: list[dict[str, int | str]]

segments instance-attribute ¤

segments: list[dict[str, int | str]]

is_pic property ¤

is_pic: bool

is_pie property ¤

is_pie: bool

update ¤

update() -> None

read ¤

read(typ: T, address: int, blob: bytearray | None = None) -> T

get_elf_info ¤

get_elf_info(filepath: str) -> ELFInfo

Parse and return ELFInfo.

Adds various calculated properties to the ELF header, segments and sections. Such added properties are those with prefix 'x_' in the returned dicts.

get_elf_info_rebased ¤

get_elf_info_rebased(filepath: str, vaddr: int) -> ELFInfo

Parse and return ELFInfo with all virtual addresses rebased to vaddr

get_containing_segments ¤

get_containing_segments(elf_filepath: str, elf_loadaddr: int, vaddr: int)

get_containing_sections ¤

get_containing_sections(elf_filepath: str, elf_loadaddr: int, vaddr: int)

dump_section_by_name ¤

dump_section_by_name(
    filepath: str, section_name: str, try_local_path: bool = False
) -> tuple[int, int, bytes] | None

Dump the content of a section from an ELF file, return the start address, size and content.

dump_relocations_by_section_name ¤

dump_relocations_by_section_name(
    filepath: str, section_name: str, try_local_path: bool = False
) -> tuple[Relocation, ...] | None

Dump the relocation entries of a section from an ELF file, return a generator of Relocation objects.

exe ¤

exe() -> Ehdr | None

Return a loaded ELF header object pointing to the Ehdr of the main executable.

entry ¤

entry() -> int

Return the address of the entry point for the main executable.

load ¤

load(pointer: int) -> Ehdr | None

reset_ehdr_type_loaded ¤

reset_ehdr_type_loaded() -> None

get_ehdr ¤

get_ehdr(pointer: int) -> tuple[int | None, Ehdr | None]

Returns an ehdr object for the ELF pointer points into.

We expect the pointer to be an address from the binary.

get_phdrs ¤

get_phdrs(pointer: int)

Returns a tuple containing (phnum, phentsize, gdb.Value), where the gdb.Value object is an ELF Program Header with the architecture-appropriate structure type.

iter_phdrs ¤

iter_phdrs(ehdr: Ehdr)

map ¤

map(pointer: int, objfile: str = '') -> tuple[Page, ...]

Given a pointer into an ELF module, return a list of all loaded sections in the ELF.

Returns:

  • tuple[Page, ...]

    A sorted list of pwndbg.lib.memory.Page objects

Example:

>>> pwndbg.aglib.elf.load(pwndbg.aglib.regs.pc)
[Page('400000-4ef000 r-xp 0'),
 Page('6ef000-6f0000 r--p ef000'),
 Page('6f0000-6ff000 rw-p f0000')]
>>> pwndbg.aglib.elf.load(0x7ffff77a2000)
[Page('7ffff75e7000-7ffff77a2000 r-xp 0x1bb000 0'),
 Page('7ffff77a2000-7ffff79a2000 ---p 0x200000 1bb000'),
 Page('7ffff79a2000-7ffff79a6000 r--p 0x4000 1bb000'),
 Page('7ffff79a6000-7ffff79ad000 rw-p 0x7000 1bf000')]

map_inner ¤

map_inner(ei_class: int, ehdr: Ehdr, objfile: str) -> tuple[Page, ...]