Library:fishhook
| 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
- https://github.com/facebook/fishhook
- https://github.com/facebook/fishhook @
aadc161ac3b80db07a9908851839a17ba63a9eb1· license: BSD-3-Clause
fishhook.c: Mach-O lazy/non-lazy symbol pointer rebinding.
Tested on: source read, not run: source read, not run · checked 2026-10-18; last activity 2024-08-01