Objective-C


Introduction

This module enables inspecting Objective-C metadata within a Mach-O binary.

macho: lief.MachO.Binary = ...
metadata: lief.objc.Metadata = macho.objc_metadata
if metadata is not None:
    print("Objective-C metadata found")
At this point, one can use the API exposed by the class to inspect the Objective-C metadata.
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 = lief.parse("some_macho")
metadata: lief.objc.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

When performing binary analysis, it can be useful to generate header-like information to get a global overview of the structures present in the Objective-C metadata.

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