Examples#
Below is a list of usage examples and small snippets that illustrate how this library can be utilized.
Objective-C Class-Dump#
The class-dump of Objective-C classes is rather easy. You can follow the next script to dump all defined classes in a binary:
1from lief import MachO
2from umbrella.objc import ObjCMetadata, ObjCDumper
3
4binary = MachO.parse("/path/to/binary").take(MachO.CPU_TYPES.ARM64)
5metadata = ObjcMetadata(binary)
6dumper = ObjCDumper()
7
8for objc_class in metadata.classes:
9 with open(f"{objc_class.name}.h", "w", encoding="utf-8") as fp:
10 dumper.dump_class(objc_class, fp)
Swift Class-Dump#
The following code-snippet may be used as an entrypoint for writing complete class-dumps of Swift metadata.
1from lief import MachO # actually, PE and ELF are supported as well
2from umbrella.swift import ReflectionContext, has_swift_metadata, SwiftDumper
3
4binary = MachO.parse("/path/to/binary").take(MachO.CPU_TYPES.ARM64)
5if has_swift_metadata(binary):
6 # To be sure a binary stores swift metadata, use has_swift_metadata(...)
7 metadata = ReflectionContext(binary)
8 dumper = SwiftDumper()
9 # Unfortunately, all types (classes, structs, enums, protocols, ...) are
10 # stores within one large array. We have to filter them:
11 for cls in metadata.classes():
12 name: str = cls.get_mangled_name()
13 with open(f"{name}.h", "w", encoding="utf-8") as fp:
14 dumper.dump_class(cls, fp=fp)
A sample output may be the following:
1public class Bar {
2 public func x() -> String {...}
3 public func y() -> Int64 {...}
4}
5
6public class Foo: Bar {
7 public let i = 0;
8 var someLongVariableName = 1;
9 private func indexed() -> Int {...}
10 public override func x() -> String {...}
11 public override func y() -> Int64 {...}
12}
1public class main.Bar {
2 // Methods
3 /* 0x1270 */ public func x() -> Swift.String
4 /* 0x12a0 */ public func y() -> Swift.Int64
5 /* 0x1320 */ static func <stripped> // Init
6}
7public class main.Foo: ? {
8 // Properties/Fields
9 let i: Swift.Int
10 var someLongVariableName: Swift.Int
11
12 // Methods
13 /* 0x13a0 */ func someLongVariableName.getter : Swift.Int // (stripped)
14 /* 0x13f0 */ func someLongVariableName.setter : Swift.Int // (stripped)
15 /* 0x1440 */ func someLongVariableName.modify : Swift.Int // (stripped)
16 /* 0x14b0 */ func <stripped> // Method
17 // Overridden functions
18 /* 0x14cc */ public override func x() -> Swift.String // from main.Bar
19 /* 0x14fc */ public override func y() -> Swift.Int64 // from main.Bar
20 /* 0x151c */ static override func <stripped> // Init from main.Bar
21}