Skip to content

Commit 829875a

Browse files
committed
Add support for macos mojave.
1 parent 2413e3e commit 829875a

File tree

2 files changed

+61
-49
lines changed

2 files changed

+61
-49
lines changed

README.md

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
PLTHook
2-
=======
1+
# PLTHook
32

43
[![tests](https://github.com/kubo/plthook/actions/workflows/run-tests.yml/badge.svg)](https://github.com/kubo/plthook/actions/workflows/run-tests.yml)
54

6-
What is plthook.
7-
----------------
5+
## What is plthook.
86

97
A utility library to hook library function calls issued by
108
specified object files (executable and libraries). This modifies
@@ -68,8 +66,15 @@ Unixes. The fourth argument of `plthook_replace()` isn't available on Unixes
6866
because it doesn't set the address of the original before the address in the PLT
6967
entry is resolved.
7068

71-
Changes
72-
-------
69+
## Changes
70+
71+
**2025-05-14:** Add support again for macOS 10.14 Mojave or before.
72+
73+
**2025-05-09:** Add loongarch64 support. (plthook_elf.c)
74+
75+
**2025-04-15:** Add s390x support. (plthook_elf.c)
76+
77+
**2025-03-07:** Add support for hooking delayed load libraries on Windows.
7378

7479
**2024-09-02:** Fix issues on macOS ([#48])
7580

@@ -81,7 +86,7 @@ Changes
8186

8287
**2022-08-12:** Support LC_DYLD_CHAINED_FIXUPS on macOS intel
8388

84-
**2020-03-30:** Check _start also in plthook_open_by_handle() (plthook_elf.c) ([#29])
89+
**2020-03-30:** Check \_start also in plthook_open_by_handle() (plthook_elf.c) ([#29])
8590

8691
**2020-03-09:** Add support for uClibc. ([#28])
8792

@@ -112,8 +117,7 @@ hooking a [prelinked file](https://en.wikipedia.org/wiki/Prelink#Linux) on Linux
112117

113118
**2017-09-18:** Fixed for processes on [valgrind](https://valgrind.org) on Linux.
114119

115-
Usage
116-
-----
120+
## Usage
117121

118122
If you have a library `libfoo.so.1` and want to intercept
119123
a function call `recv()` without modifying the library,
@@ -127,17 +131,17 @@ in your source tree and add the following code.
127131
static ssize_t my_recv(int sockfd, void *buf, size_t len, int flags)
128132
{
129133
ssize_t rv;
130-
134+
131135
... do your task: logging, etc. ...
132136
rv = recv(sockfd, buf, len, flags); /* call real recv(). */
133137
... do your task: logging, check received data, etc. ...
134138
return rv;
135139
}
136-
140+
137141
int install_hook_function()
138142
{
139143
plthook_t *plthook;
140-
144+
141145
if (plthook_open(&plthook, "libfoo.so.1") != 0) {
142146
printf("plthook_open error: %s\n", plthook_error());
143147
return -1;
@@ -163,17 +167,17 @@ static ssize_t (*recv_func)(int sockfd, void *buf, size_t len, int flags);
163167
static ssize_t my_recv(int sockfd, void *buf, size_t len, int flags)
164168
{
165169
ssize_t rv;
166-
170+
167171
... do your task: logging, etc. ...
168172
rv = (*recv_func)(sockfd, buf, len, flags); /* call real recv(). */
169173
... do your task: logging, check received data, etc. ...
170174
return rv;
171175
}
172-
176+
173177
int install_hook_function()
174178
{
175179
plthook_t *plthook;
176-
180+
177181
if (plthook_open_by_address(&plthook, &recv_func) != 0) {
178182
printf("plthook_open error: %s\n", plthook_error());
179183
return -1;
@@ -203,8 +207,7 @@ For example `api-ms-win-shcore-path-l1-1-0.dll:@170`.
203207

204208
[ordinal]: https://msdn.microsoft.com/en-us/library/e7tsx612.aspx
205209

206-
Another Usage
207-
-------------
210+
## Another Usage
208211

209212
PLTHook provides a function enumerating PLT/IAT entries.
210213

@@ -228,25 +231,23 @@ void print_plt_entries(const char *filename)
228231
}
229232
```
230233
231-
Supported Platforms
232-
-------------------
233-
234-
| Platform | source file | status |
235-
| -------- | ----------- | ------ |
236-
| Linux i386 and x86_64 | plthook_elf.c | tested using [github actions] |
237-
| Linux arm, aarch64, powerpc and powerpc64le | plthook_elf.c | tested on [QEMU][] using [github actions] |
238-
| Windows 32-bit and x64 (MSVC) | plthook_win32.c | tested using [github actions] |
239-
| macOS (intel) (*4) | plthook_osx.c | tested using [github actions] |
240-
| macOS (arm) | plthook_osx.c | tested using [github actions] |
241-
| Windows 32-bit and x64 (Mingw32 and Cygwin) | plthook_win32.c | perhaps(*2) |
242-
| Solaris x86_64 | plthook_elf.c | perhaps(*1) |
243-
| FreeBSD i386 and x86_64 except i386 program on x86_64 OS | plthook_elf.c | perhaps(*1) |
244-
| Android(*3) | plthook_elf.c | perhaps(*2) |
234+
## Supported Platforms
235+
236+
| Platform | source file | status |
237+
| --------------------------------------------------------------------- | --------------- | ----------------------------------------- |
238+
| Linux i386 and x86_64 | plthook_elf.c | tested using [github actions] |
239+
| Linux arm, armhf, arm64, ppc, ppc64le, riscv64, s390x and loongarch64 | plthook_elf.c | tested on [QEMU][] using [github actions] |
240+
| Windows 32-bit and x64 (MSVC) | plthook_win32.c | tested using [github actions] |
241+
| macOS (intel) | plthook_osx.c | tested using [github actions] |
242+
| macOS (arm) | plthook_osx.c | tested using [github actions] |
243+
| Windows 32-bit and x64 (Mingw32 and Cygwin) | plthook_win32.c | perhaps(\*2) |
244+
| Solaris x86_64 | plthook_elf.c | perhaps(\*1) |
245+
| FreeBSD i386 and x86_64 except i386 program on x86_64 OS | plthook_elf.c | perhaps(\*1) |
246+
| Android(\*3) | plthook_elf.c | perhaps(\*2) |
245247
246248
*1 Tested on a local VM before.
247249
*2 Tested on travis-ci.org before.
248-
*3 Contributed by [Daniel Deptford][].
249-
*4 macOS 10.14 Mojave support was dropped on 2022-09-19.
250+
\*3 Contributed by [Daniel Deptford][].
250251
251252
[QEMU]: http://www.qemu.org/
252253
[Daniel Deptford]: https://github.com/redmercury
@@ -262,7 +263,6 @@ Supported Platforms
262263
[#48]: https://github.com/kubo/plthook/issues/48
263264
[github actions]: https://github.com/kubo/plthook/actions/workflows/run-tests.yml
264265
265-
License
266-
-------
266+
## License
267267
268268
2-clause BSD-style license.

plthook_osx.c

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@
4040
#include <unistd.h>
4141
#include <inttypes.h>
4242
#include <dlfcn.h>
43-
#include <errno.h>
43+
#include <sys/mman.h>
4444
#include <mach/mach.h>
4545
#include <mach-o/dyld.h>
4646
#include <sys/mman.h>
47+
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 120000
48+
#include <errno.h>
4749
#include <mach-o/fixup-chains.h>
50+
#endif
4851
#include "plthook.h"
4952

5053
#ifdef PLTHOOK_DEBUG
@@ -188,11 +191,14 @@ typedef struct {
188191
static int plthook_open_real(plthook_t **plthook_out, uint32_t image_idx, const struct mach_header *mh, const char *image_name);
189192
static unsigned int set_bind_addrs(data_t *data, unsigned int idx, uint32_t bind_off, uint32_t bind_size, char weak);
190193
static void set_bind_addr(data_t *d, unsigned int *idx, const char *sym_name, int seg_index, int seg_offset, int addend, char weak);
194+
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 120000
191195
static int read_chained_fixups(data_t *d, const char *image_name);
196+
static uint8_t *fileoff_to_vmaddr(data_t *data, size_t offset);
192197
#ifdef PLTHOOK_DEBUG_FIXUPS
193198
static const char *segment_name_from_addr(data_t *d, size_t addr);
194199
static const char *section_name_from_addr(data_t *d, size_t addr);
195200
#endif
201+
#endif
196202

197203
static int set_mem_prot(plthook_t *plthook);
198204
static int get_mem_prot(plthook_t *plthook, void *addr);
@@ -202,7 +208,6 @@ static inline uint8_t *fileoff_to_vmaddr_in_segment(data_t *d, int segment_index
202208
const struct segment_command_64 *seg = d->segments[segment_index];
203209
return (uint8_t *)(seg->vmaddr - seg->fileoff + d->slide + offset);
204210
}
205-
static uint8_t *fileoff_to_vmaddr(data_t *data, size_t offset);
206211

207212
static void set_errmsg(const char *fmt, ...) __attribute__((__format__ (__printf__, 1, 2)));
208213

@@ -535,10 +540,15 @@ static int plthook_open_real(plthook_t **plthook_out, uint32_t image_idx, const
535540
return PLTHOOK_INVALID_FILE_FORMAT;
536541
}
537542
if (data.chained_fixups != NULL) {
543+
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 120000
538544
int rv = read_chained_fixups(&data, image_name);
539545
if (rv != 0) {
540546
return rv;
541547
}
548+
#else
549+
set_errmsg("failed to read chained fixups segment, this is only available after macOS 12 Monterrey: %s", image_name);
550+
return PLTHOOK_INTERNAL_ERROR;
551+
#endif
542552
} else {
543553
nbind = 0;
544554
nbind = set_bind_addrs(&data, nbind, dyld_info->bind_off, dyld_info->bind_size, 0);
@@ -669,6 +679,7 @@ static void set_bind_addr(data_t *data, unsigned int *idx, const char *sym_name,
669679
(*idx)++;
670680
}
671681

682+
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 120000
672683
typedef struct {
673684
const char *image_name;
674685
FILE *fp;
@@ -959,6 +970,18 @@ static int read_chained_fixups(data_t *d, const char *image_name)
959970
return rv;
960971
}
961972

973+
static uint8_t *fileoff_to_vmaddr(data_t *d, size_t offset)
974+
{
975+
int i;
976+
for (i = 0; i < d->num_segments; i++) {
977+
const struct segment_command_64 *seg = d->segments[i];
978+
if (seg->fileoff <= offset && offset < seg->fileoff + seg->filesize) {
979+
return fileoff_to_vmaddr_in_segment(d, i, offset);
980+
}
981+
}
982+
return NULL;
983+
}
984+
962985
#ifdef PLTHOOK_DEBUG_FIXUPS
963986
static const char *segment_name_from_addr(data_t *d, size_t addr)
964987
{
@@ -984,6 +1007,7 @@ static const char *section_name_from_addr(data_t *d, size_t addr)
9841007
return "?";
9851008
}
9861009
#endif
1010+
#endif
9871011

9881012
static int set_mem_prot(plthook_t *plthook)
9891013
{
@@ -1037,18 +1061,6 @@ static int get_mem_prot(plthook_t *plthook, void *addr)
10371061
return 0;
10381062
}
10391063

1040-
static uint8_t *fileoff_to_vmaddr(data_t *d, size_t offset)
1041-
{
1042-
int i;
1043-
for (i = 0; i < d->num_segments; i++) {
1044-
const struct segment_command_64 *seg = d->segments[i];
1045-
if (seg->fileoff <= offset && offset < seg->fileoff + seg->filesize) {
1046-
return fileoff_to_vmaddr_in_segment(d, i, offset);
1047-
}
1048-
}
1049-
return NULL;
1050-
}
1051-
10521064
int plthook_enum(plthook_t *plthook, unsigned int *pos, const char **name_out, void ***addr_out)
10531065
{
10541066
plthook_entry_t entry;

0 commit comments

Comments
 (0)