Skip to content

Commit 8dbd09b

Browse files
committed
Merge pull request facebook#18 from uroboro/master
Save previous pointer automatically
2 parents 218b6af + 80e1212 commit 8dbd09b

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ Once you add `fishhook.h`/`fishhook.c` to your project, you can rebind symbols a
1818
static int (*orig_close)(int);
1919
static int (*orig_open)(const char *, int, ...);
2020

21-
void save_original_symbols() {
22-
orig_close = dlsym(RTLD_DEFAULT, "close");
23-
orig_open = dlsym(RTLD_DEFAULT, "open");
24-
}
25-
2621
int my_close(int fd) {
2722
printf("Calling real close(%d)\n", fd);
2823
return orig_close(fd);
@@ -48,8 +43,7 @@ int my_open(const char *path, int oflag, ...) {
4843
int main(int argc, char * argv[])
4944
{
5045
@autoreleasepool {
51-
save_original_symbols();
52-
rebind_symbols((struct rebinding[2]){{"close", my_close}, {"open", my_open}}, 2);
46+
rebind_symbols((struct rebinding[2]){{"close", my_close, (void *)&orig_close}, {"open", my_open, (void *)&orig_open}}, 2);
5347

5448
// Open our own binary and print out first 4 bytes (which is the same
5549
// for all Mach-O binaries on a given architecture)

fishhook.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ static void perform_rebinding_with_section(struct rebindings_entry *rebindings,
9797
for (uint j = 0; j < cur->rebindings_nel; j++) {
9898
if (strlen(symbol_name) > 1 &&
9999
strcmp(&symbol_name[1], cur->rebindings[j].name) == 0) {
100+
if (cur->rebindings[j].replaced != NULL &&
101+
indirect_symbol_bindings[i] != cur->rebindings[j].replacement) {
102+
*(cur->rebindings[j].replaced) = indirect_symbol_bindings[i];
103+
}
100104
indirect_symbol_bindings[i] = cur->rebindings[j].replacement;
101105
goto symbol_loop;
102106
}

fishhook.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extern "C" {
3838
struct rebinding {
3939
const char *name;
4040
void *replacement;
41+
void **replaced;
4142
};
4243

4344
/*

0 commit comments

Comments
 (0)