Functions

The following functions are available globally.

  • Hook an Objective-C method

    Note

    Libraries often need to hook Objective-C methods. This provides an easy way to hook the method and call to the original method. This also provides a guarantee that hooking a class that doesn’t implement a method won’t overwrite the method in the super class.

    Declaration

    Objective-C

    enum LIBHOOKER_ERR LBHookMessage(Class objcClass, SEL selector,
                                     void *replacement, void *old_ptr)

    Parameters

    class

    The Objective C class to hook

    selector

    The Objective C selector to hook

    replacement

    A pointer to the replacement implementation that gets called instead of the original function

    old_ptr

    A pointer to the original implementation that can be called if needed

    Return Value

    Returns any errors that may have taken place when hooking the method

  • Get a human readable string for debugging purposes.

    Declaration

    Objective-C

    const char *LHStrError(enum LIBHOOKER_ERR err)

    Parameters

    err

    The raw error value.

    Return Value

    A human-readable error string, or “Unknown Error” on invalid error.

  • Open a dylib for use with LHFindSymbols

    Note

    dyld handles do not provide enough information to grab symbols. Use this to have libhooker load the dylib.

    Declaration

    Objective-C

    struct libhooker_image *LHOpenImage(const char *path)

    Parameters

    path

    The path to the image on disk or in the shared cache.

    Return Value

    A handle to the dylib that may then be used with LHFindSymbols to find symbols.

  • Close and free a dylib handle opened with LHOpenImage

    Declaration

    Objective-C

    void LHCloseImage(struct libhooker_image *libhookerImage)

    Parameters

    libhookerImage

    The libhooker handle to the image on disk or in the shared cache.

  • Search for symbols within a dylib either on disk or in the dyld shared cache.

    Note

    dyld often is not able to find symbols that are present but not marked as public and/or exported. Also often the dyld shared cache has symbols replaced with <redacted> when loaded into memory.

    Declaration

    Objective-C

    _Bool LHFindSymbols(struct libhooker_image *libhookerImage,
                        const char **symbolNames, void **searchSyms,
                        size_t searchSymCount)

    Parameters

    libhookerImage

    The libhooker handle to the image on disk or in the shared cache

    symbolNames

    An array of the symbol names to search for

    searchSyms

    An array that gets populated with the symbols found

    searchSymCount

    The number of symbols to search for

    Return Value

    Returns true if all symbols were found

  • Creates an executable page from raw instruction data

    Note

    Sometimes a library may generate or load new code, but may simply want an executable page with this new code.

    Declaration

    Objective-C

    _Bool LHExecMemory(void **page, void *data, size_t size)

    Parameters

    page

    A pointer that gets written to with the address of code in the new memory page

    data

    An array of ARM64 opcodes to write to this new memory page.

    size

    The size of data

    Return Value

    Returns true if the memory was created. Returns false otherwise.

  • Patch memory (that may often be read-only or read/execute-only)

    Note

    Sometimes low level memory access is required to hook parts of applications. This function provides an easy way for libraries to do so, with a relatively safe guarantee of preserving memory permissions, and re-mapping the memory if required.

    Declaration

    Objective-C

    int LHPatchMemory(const struct LHMemoryPatch *patches, int count)

    Parameters

    patches

    A pointer to the destination to write to

    count

    The number of memory regions to patch

    Return Value

    Returns any errors that may have taken place while hooking the functions

  • Hook Functions in memory

    note: Libraries often need to hook functions in memory. This provides an easy way to hook the function and get back a pointer to the original function, should you need to call it.

    Declaration

    Objective-C

    int LHHookFunctions(const struct LHFunctionHook *hooks, int count)

    Parameters

    hooks

    An array of LHFunctionHook describing each function to hook

    count

    The number of functions to hook

    Return Value

    Returns any errors that may have taken place while hooking the functions