Objective-C


Introduction

This module allows to inspect Objective-C metadata from 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")
Then 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 doing binary analysis it can useful to generate header-like information to get a global overview of the different 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 different levels:

From a technical standpoint, this output is generated by generating a Clang AST and by applying the LLVM’s printer visitor on this AST.

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 tweak the generated output. For instance, we can remove the commented address associated with the objective-c method with 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