Skip to content

Commit

Permalink
Merge pull request facebook#18 from uroboro/master
Browse files Browse the repository at this point in the history
Save previous pointer automatically
  • Loading branch information
grp committed Oct 28, 2015
2 parents 218b6af + 80e1212 commit 8dbd09b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ Once you add `fishhook.h`/`fishhook.c` to your project, you can rebind symbols a
static int (*orig_close)(int);
static int (*orig_open)(const char *, int, ...);

void save_original_symbols() {
orig_close = dlsym(RTLD_DEFAULT, "close");
orig_open = dlsym(RTLD_DEFAULT, "open");
}

int my_close(int fd) {
printf("Calling real close(%d)\n", fd);
return orig_close(fd);
Expand All @@ -48,8 +43,7 @@ int my_open(const char *path, int oflag, ...) {
int main(int argc, char * argv[])
{
@autoreleasepool {
save_original_symbols();
rebind_symbols((struct rebinding[2]){{"close", my_close}, {"open", my_open}}, 2);
rebind_symbols((struct rebinding[2]){{"close", my_close, (void *)&orig_close}, {"open", my_open, (void *)&orig_open}}, 2);

// Open our own binary and print out first 4 bytes (which is the same
// for all Mach-O binaries on a given architecture)
Expand Down
4 changes: 4 additions & 0 deletions fishhook.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ static void perform_rebinding_with_section(struct rebindings_entry *rebindings,
for (uint j = 0; j < cur->rebindings_nel; j++) {
if (strlen(symbol_name) > 1 &&
strcmp(&symbol_name[1], cur->rebindings[j].name) == 0) {
if (cur->rebindings[j].replaced != NULL &&
indirect_symbol_bindings[i] != cur->rebindings[j].replacement) {
*(cur->rebindings[j].replaced) = indirect_symbol_bindings[i];
}
indirect_symbol_bindings[i] = cur->rebindings[j].replacement;
goto symbol_loop;
}
Expand Down
1 change: 1 addition & 0 deletions fishhook.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern "C" {
struct rebinding {
const char *name;
void *replacement;
void **replaced;
};

/*
Expand Down

0 comments on commit 8dbd09b

Please sign in to comment.