|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, Charlie Curtsinger and Emery Berger, |
| 3 | + * University of Massachusetts Amherst |
| 4 | + * This file is part of the Coz project. See LICENSE.md file at the top-level |
| 5 | + * directory of this distribution and at http://github.com/plasma-umass/coz. |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef COZ_MAC_INTERPOSE_H |
| 9 | +#define COZ_MAC_INTERPOSE_H |
| 10 | + |
| 11 | +#ifdef __APPLE__ |
| 12 | + |
| 13 | +/** |
| 14 | + * macOS DYLD interposition support. |
| 15 | + * |
| 16 | + * On macOS, function interposition is done via DYLD_INSERT_LIBRARIES. |
| 17 | + * This header provides macros for creating interposition tuples that |
| 18 | + * tell the dynamic linker which functions to replace. |
| 19 | + * |
| 20 | + * The interposition mechanism uses the __DATA,__interpose section. |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * Structure used by dyld for interposition. |
| 25 | + * When placed in the __DATA,__interpose section, dyld will replace |
| 26 | + * calls to 'original' with calls to 'replacement'. |
| 27 | + */ |
| 28 | +typedef struct { |
| 29 | + const void* replacement; |
| 30 | + const void* original; |
| 31 | +} interpose_tuple_t; |
| 32 | + |
| 33 | +/** |
| 34 | + * Macro to create an interposition entry. |
| 35 | + * Usage: INTERPOSE(my_function, original_function) |
| 36 | + * |
| 37 | + * This creates a static entry in the __DATA,__interpose section that |
| 38 | + * tells dyld to replace calls to 'original_function' with 'my_function'. |
| 39 | + */ |
| 40 | +#define INTERPOSE(replacement, original) \ |
| 41 | + __attribute__((used, section("__DATA,__interpose"))) \ |
| 42 | + static const interpose_tuple_t interpose_##original = { \ |
| 43 | + (const void*)(unsigned long)&replacement, \ |
| 44 | + (const void*)(unsigned long)&original \ |
| 45 | + } |
| 46 | + |
| 47 | +/** |
| 48 | + * Alternative macro using dyld_interposing_tuple from mach-o/dyld-interposing.h |
| 49 | + * if available. This is the official Apple mechanism. |
| 50 | + */ |
| 51 | +#ifdef DYLD_INTERPOSE |
| 52 | +#undef INTERPOSE |
| 53 | +#define INTERPOSE(replacement, original) DYLD_INTERPOSE(replacement, original) |
| 54 | +#endif |
| 55 | + |
| 56 | +#endif // __APPLE__ |
| 57 | + |
| 58 | +#endif // COZ_MAC_INTERPOSE_H |
0 commit comments