ELF


Introduction

import lief

# Using filepath
elf: lief.ELF.Binary = lief.ELF.parse("/bin/ls")

# Using a Path from pathlib
elf: lief.ELF.Binary = lief.ELF.parse(pathlib.Path(r"C:\Users\test.elf"))

# Using a io object
with open("/bin/ssh", 'rb') as f:
  elf: lief.ELF.Binary = lief.ELF.parse(f)

Note

In Python, you can also use lief.parse() which returns a lief.ELF.Binary object.

From this parsed ELF binary you can use all the API exposed by the object to inspect or modify the binary itself.
elf: lief.ELF.Binary = ...

print(elf.header.entrypoint)

for section in elf.sections:
    print(section.name, len(section.content))
elf: lief.ELF.Binary = ...

elf.add_library("libdemo.so")
elf.write("new.elf")

Advance Parsing/Writing

Warning

Generally speaking, requires a complete initial parsing of the ELF file.
parser_config = lief.ELF.ParserConfig()
parser_config.parse_overlay = False

elf: lief.ELF.Binary = lief.ELF.parse("my.elf", parser_config)

builder_config = lief.ELF.Builder.config_t()
builder_config.gnu_hash = False

elf.write("new.elf", builder_config)

DWARF Support

Note that this support is only available in the extended version of LIEF.