Skip to content

can fishhook hook c function written by ourself? #25

Closed
@intheway

Description

@intheway

myFunction is a c function written by me in my project, I want to hook it, but I tried this code failed.

int myFunction(int a){
    return a+1;
}

static int (*orig_myFunction)(int);

int hook_myFunction(int a){
    return a+2;
}

int main(int argc, char * argv[])
{
    @autoreleasepool {
        rebind_symbols((struct rebinding[1]){{"myFunction", hook_myFunction, (void *)&orig_myFunction}}, 1);
        printf("%d\n" , myFunction(1));
       return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

This means fishhook can only hook system api or how to hook c function written by ourself ?
Thanks.

Activity

fpotter

fpotter commented on Mar 7, 2016

@fpotter

fishhook can only hook functions that exist in other libraries. It cannot
hook functions that exist in the same image (library or executable) as your
currently running code.

The reason for this is that there's no indirection that happens when you
call a function in your own executable. It's just a plain jump to another
code address in your executable.

That's very different from calling a function in an external library, where
your executable uses dyld to figure out the address of the function being
called before jumping to it.

On Sun, Mar 6, 2016 at 10:59 PM, Zhengwei Yin notifications@github.com
wrote:

myFunction is a c function written by me in my project, I want to hook
it, but I tried this code failed.

int myFunction(int a){
return a+1;
}

static int (*orig_myFunction)(int);

int hook_myFunction(int a){
return a+2;
}

int main(int argc, char * argv[])
{
@autoreleasepool {
rebind_symbols((struct rebinding[1]){{"myFunction", hook_myFunction, (void *)&orig_myFunction}}, 1);
printf("%d\n" , myFunction(1));
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}

This means fishhook can only hook system api or how to hook c function
written by ourself ?
Thanks.


Reply to this email directly or view it on GitHub
#25.

intheway

intheway commented on Mar 7, 2016

@intheway
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @fpotter@intheway

        Issue actions

          can fishhook hook c function written by ourself? · Issue #25 · facebook/fishhook