Objective-C

Introduction

LIEF Extended reads Objective-C classes, methods, properties, and protocols from Mach-O metadata. It can also generate header-like declarations.

This information comes from Objective-C runtime metadata in the file, so it can be present even when source-level debug information is unavailable.

Inspect metadata

macho: lief.MachO.Binary

metadata = macho.objc_metadata
if metadata is not None:
    print("Objective-C metadata found")
When metadata is present, use to inspect it. In particular, the function can be used to generate a header-like output of all the Objective-C metadata found in the binary.
macho: lief.MachO.Binary

metadata = macho.objc_metadata
for clazz in metadata.classes:
    print(f"name={clazz.name}")
    for meth in clazz.methods:
        print(f"  method.name={meth.name}")
print(metadata.to_decl())

Class Dump

Generate header-like declarations to review the classes and protocols found in the binary. The output describes the recorded metadata, including method signatures. It does not recover method implementations or the original headers.

Objective-C class dump based on LIEF & LLVM

LIEF provides a way to generate this header-like information at various levels:

Technically, this output is created by generating a Clang AST and applying the LLVM printer visitor to it.

fn classdump(macho: &lief::macho::Binary) {
    let metadata = macho.objc_metadata().expect("Missing Objective-C info");
    for class in metadata.classes() {
        println!("{}", class.to_decl());
    }
}
The can be used to customize the generated output. For example, we can remove the commented addresses associated with Objective-C methods using this option:
def print_without_address(macho: lief.MachO.Binary):

    metadata = macho.objc_metadata

    config = lief.objc.DeclOpt()
    config.show_annotations = False

    for cls in metadata.classes:
        print(cls.to_decl(config))


References

API

You can find the documentation of the API for the different languages here:

Python API

C++ API

Rust API: lief::objc