Skip to content

Commit

Permalink
Add support for extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Jul 13, 2024
1 parent f31c8ff commit b2b7fb9
Show file tree
Hide file tree
Showing 265 changed files with 15,618 additions and 56 deletions.
2 changes: 2 additions & 0 deletions api/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ if(APPLE)
)
endif()


if (UNIX)
if (LIEF_PYTHON_STATIC)
set_target_properties(pyLIEF PROPERTIES SUFFIX ".a")
Expand All @@ -89,6 +90,7 @@ elseif(WIN32)
endif()
endif()


if (WIN32)
target_link_libraries(pyLIEF PUBLIC ${PYTHON_LIBRARIES})
endif()
Expand Down
66 changes: 66 additions & 0 deletions api/python/examples/objc_dump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python
'''
This script dumps the Objective-C metadata from the provided binary and
generate a header-like of different structure identified.
Note: this script is only working with the extended version of LIEF
'''

import sys
import lief
import argparse
from pathlib import Path
from typing import Optional

def process(filepath: str, skip_protocols: bool = False,
output_path: Optional[str] = None) -> int:
target = Path(filepath)
if not target.is_file():
print(f"'{target}' is not a valid file", file=sys.stderr)
return 1

macho = lief.MachO.parse(target)
if macho is None:
print(f"Can't parse Mach-O file: {target}", file=sys.stderr)
metadata = macho.at(0).objc_metadata

if metadata is None:
print(f"Can't parse ObjC metadata in {target}'", file=sys.stderr)
return 1

if skip_protocols:
output = ""
for cls in metadata.classes:
output += cls.to_decl()
else:
output = metadata.to_decl()
print(output)

if output_path is not None:
out = Path(output_path)
if out.is_dir():
out /= f"{target.name}_objc.h"
out.write_text(output)
print(f"Saved in {out}")
else:
print(f"Saved in {out}")
out.write_text(output)
return 0


def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument('-o', '--output',
help='Output file',
default=None)
parser.add_argument('--skip-protocols',
help='Skip ObjC protocols definition',
action='store_true')
parser.add_argument("file", help='Mach-O file')
args = parser.parse_args()

lief.logging.set_level(lief.logging.LEVEL.WARN)
return process(args.file, args.skip_protocols, args.output)

if __name__ == "__main__":
raise SystemExit(main())
23 changes: 23 additions & 0 deletions api/python/examples/pdb_inspect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import lief
import sys

pdb = lief.pdb.load(sys.argv[0])

print("arg={}, guid={}", pdb.age, pdb.guid)

for sym in pdb.public_symbols:
print("name={}, section={}, RVA={}",
sym.name, sym.section_name, sym.RVA)

for ty in pdb.types:
if isinstance(ty, lief.pdb.types.Class):
print("Class[name]={}", ty.name)

for cu in pdb.compilation_units:
print("module={}", cu.module_name)
for src in cu.sources:
print(" - {}", src)

for func in cu.functions:
print("name={}, section={}, RVA={}, code_size={}",
func.name, func.section_name, func.RVA, func.code_size)
3 changes: 3 additions & 0 deletions api/python/lief/MachO.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import lief.MachO.Section # type: ignore
import lief.MachO.SegmentCommand # type: ignore
import lief.MachO.Symbol # type: ignore
import lief.MachO.TwoLevelHints # type: ignore
import lief.objc # type: ignore
import os

class ARM64_RELOCATION:
Expand Down Expand Up @@ -297,6 +298,8 @@ class Binary(lief.Binary):
@property
def main_command(self) -> lief.MachO.MainCommand: ...
@property
def objc_metadata(self) -> Optional[lief.objc.Metadata]: ...
@property
def off_ranges(self) -> lief.MachO.Binary.range_t: ...
@property
def overlay(self) -> memoryview: ...
Expand Down
2 changes: 1 addition & 1 deletion api/python/lief/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
if len(__path__) > 0 and __path__[0] not in sys.path:
from . import _lief
from ._lief import *
from ._lief import __version__, __tag__, __commit__, __is_tagged__
from ._lief import __version__, __tag__, __commit__, __is_tagged__, __extended__

# cf. https://github.com/pytorch/pytorch/blob/60a3b7425dde97fe8b46183c154a9c3b24f0c733/torch/__init__.py#L467-L470
for attr in dir(_lief):
Expand Down
39 changes: 38 additions & 1 deletion api/python/lief/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from typing import Any, ClassVar, Optional, Union

from . import ART, Android, DEX, ELF, MachO, OAT, PE, VDEX, logging # type: ignore
from . import ART, Android, DEX, ELF, MachO, OAT, PE, VDEX, dwarf, logging, objc, pdb # type: ignore
from typing import overload
import io
import lief # type: ignore
import lief.Binary # type: ignore
import lief.DebugInfo # type: ignore
import lief.ELF # type: ignore
import lief.Function # type: ignore
import lief.MachO # type: ignore
Expand Down Expand Up @@ -109,6 +110,8 @@ class Binary(Object):
@property
def ctor_functions(self) -> list[lief.Function]: ...
@property
def debug_info(self) -> lief.DebugInfo: ...
@property
def entrypoint(self) -> int: ...
@property
def exported_functions(self) -> list[lief.Function]: ...
Expand All @@ -135,6 +138,28 @@ class Binary(Object):
@property
def symbols(self) -> lief.Binary.it_symbols: ...

class DebugInfo:
class FORMAT:
DWARF: ClassVar[DebugInfo.FORMAT] = ...
PDB: ClassVar[DebugInfo.FORMAT] = ...
UNKNOWN: ClassVar[DebugInfo.FORMAT] = ...
__name__: str
def __init__(self, *args, **kwargs) -> None: ...
@staticmethod
def from_value(arg: int, /) -> lief.DebugInfo.FORMAT: ...
def __ge__(self, other) -> bool: ...
def __gt__(self, other) -> bool: ...
def __hash__(self) -> int: ...
def __index__(self) -> Any: ...
def __int__(self) -> int: ...
def __le__(self, other) -> bool: ...
def __lt__(self, other) -> bool: ...
@property
def value(self) -> int: ...
def __init__(self, *args, **kwargs) -> None: ...
@property
def format(self) -> lief.DebugInfo.FORMAT: ...

class ENDIANNESS:
BIG: ClassVar[ENDIANNESS] = ...
LITTLE: ClassVar[ENDIANNESS] = ...
Expand Down Expand Up @@ -302,6 +327,11 @@ class Symbol(Object):
value: int
def __init__(self, *args, **kwargs) -> None: ...

class debug_location_t:
file: str
line: int
def __init__(self, *args, **kwargs) -> None: ...

class lief_errors:
asn1_bad_tag: ClassVar[lief_errors] = ...
build_error: ClassVar[lief_errors] = ...
Expand Down Expand Up @@ -342,6 +372,13 @@ class ok_t:
def __init__(self, *args, **kwargs) -> None: ...
def __bool__(self) -> bool: ...

class range_t:
high: int
low: int
def __init__(self, *args, **kwargs) -> None: ...
@property
def size(self) -> int: ...

def current_platform() -> lief.PLATFORMS: ...
def disable_leak_warning() -> None: ...
@overload
Expand Down
Loading

0 comments on commit b2b7fb9

Please sign in to comment.