Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Library:fishhook

From ReverseEngineering.wiki
Library
Platforms macOS, iOS
License BSD-3-Clause
Website https://github.com/facebook/fishhook
On this wiki Facts · Code · Notes · Topics

fishhook (BSD-3-Clause, Facebook) rebinds imported C functions in Mach-O images at runtime by rewriting lazy and non-lazy symbol pointers.

API (fishhook.h): struct rebinding { const char *name; void *replacement; void **replaced; }, rebind_symbols(rebindings, n) for all loaded and future images, rebind_symbols_image(header, slide, rebindings, n) for one image. The whole implementation is one file, fishhook.c.

Mechanism, as read in fishhook.c: for each image it reads LC_SYMTAB and LC_DYSYMTAB, scans segments named __DATA and __DATA_CONST, and for sections of type S_LAZY_SYMBOL_POINTERS or S_NON_LAZY_SYMBOL_POINTERS uses section->reserved1 as the start index into the indirect symbol table. Indices equal to INDIRECT_SYMBOL_ABS or INDIRECT_SYMBOL_LOCAL are skipped. The name is compared after the leading underscore (&symbol_name[1]). Before writing it calls vm_protect(..., VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY). The first rebind_symbols call registers _dyld_register_func_for_add_image, whose callback is also invoked for images already loaded.

Limits: only symbols imported through these pointer sections, and only for pointer slots, not calls bound statically or inlined; no effect on code that resolves with dlsym afterwards. See Technique:Function hooking/Unix and mobile, Format:Mach-O.

Code & repositories