Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8821351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:49:56+00:00 2026-06-14T05:49:56+00:00

Given a function with C bindings and an arbitrary signature, it is a simple

  • 0

Given a function with C bindings and an arbitrary signature, it is a simple matter to create a pointer to the function, pass it around, wrap it, and invoke it.

int fun(int x, int y)
{
   return x + y;
}
void* funptr()
{
   return (void*)&fun;
}
int wrapfun(int x, int y)
{
   // inject additional wrapper logic
   return ((int (*)(int, int))funptr())(x, y);
}

So long as the caller and callee follow the same calling convention and agree on the signature, everything works.

Now let’s say I want to wrap a library with thousands of functions. I can use nm or readelf to grab the names of all the functions to be wrapped, but I’d rather not have to care about the signatures, or even need to include the library’s associated header files.

In some cases, cleanly including the headers may not be an option, given cosmetic changes that occur between versions and platforms. For example:

// from openssl/ssl.h v0.9.8
SSL_CTX* SSL_CTX_new(SSL_METHOD* meth);
// from openssl/ssl.h v1.0.0
SSL_CTX* SSL_CTX_new(const SSL_METHOD* meth);

That is my background rationale, which you may leave or take. Regardless, my question is this:

Is there a way to write

// pseudocode
void wrapfun()
{
    return ((void (*)())funptr())();
}

such that the caller of wrapfun knows the signature of fun, but wrapfun itself doesn’t have to?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:49:57+00:00Added an answer on June 14, 2026 at 5:49 am

    If you look at the assembly produced from compiled C functions, you will see every function body wrapped by

    pushq %rbp
    movq  %rsp, %rbp
    ; body
    leave
    ret
    

    http://en.wikipedia.org/wiki/X86_instruction_listings lists the leave instruction as an 80186 equivalent of (in AT&T syntax)

    movq  %rbp, %rsp
    popq  %rpb
    

    So leave is just the inverse of the first two lines: save off the caller’s stack frame and create our own stack frame, then unwind at the end.

    The closing ret is the inverse of the call that got us here, and http://www.unixwiz.net/techtips/win32-callconv-asm.html shows the hidden push and pop of the instruction pointer register that occurs during these paired instructions.

    The reason the void function pointer call doesn’t work by itself, because of this assembly created for function wrapfun by the compiler. What we need to do is create the wrapper in such a way that it can hand the stack frame set up for it by the caller directly to the call of fun, without its own stack frame getting in the way. In other words, observe the C calling convention and violate it at the same time.

    Consider a C prototype

    int wrapfun(int x, int y);
    

    paired with an assembly implementation (AT&T x86_64)

      .file "wrapfun.s"
      .globl wrapfun
      .type   wrapfun, @function
    wrapfun:
      call    funptr
      jmp     *%rax
      .size   wrapfun, .-wrapfun
    

    Basically, we skip the typical stack pointer and base pointer manipulation, because we want fun‘s stack to look exactly like my stack. The call to funptr will create his own stack space and save his result into register RAX. Because we’ve got no stack space of our own, and because our caller’s IP is sitting nicely at the top of the stack, we can simply do an unconditional jump to the wrapped function, and let his ret jump all the way back. In this manner, once the function pointer is invoked, he will see the stack as it was set up by his caller.

    If we need to use local variables, pass parameters to funptr, etc, we can always set up our stack, then tear it down prior to the call:

    wrapfun:
      pushq   %rbp
      movl    %rsp, %rbp ; set up my stack
      call    funptr
      leave              ; tear down my stack
      jmp     *%rax
    

    Alternatively, we could embed this logic into inline assembly, taking advantage of our knowledge of what the compiler will do before and after:

    void wrapfun()
    {
        void* p = funptr();
        __asm__(
            "movq -8(%rbp), %rax\n\t"
            "leave\n\t"
            "popq %rbx\n\t"
            "call *%rax\n\t"
            "pushq %rbx\n\t"
            "pushq %ebp\n\t"    // repeat initial function setup
            "movq %rsp, %rbp"   // so it can be torn down correctly
        );
    }
    

    This approach has the advantage of easier declaration of C local variables prior to the magic. The last local variable declared will be at RBP-sizeof(var), and we save it in RAX prior to tearing down the stack. Another possible benefit is the opportunity to use the C preprocessesor to, for example, inline 32-bit or 64-bit assembly without requiring separate source files.

    EDIT: The disadvantage is now the requirement to save the IP into a register limits the application portability by requiring RBX to not be used by the caller.

    In short, the answer is yes. It is definitely possible to wrap a function without knowing its signature, if you are willing to get your hands a little dirty. No promises as to portability ;).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a function object, how can I get its signature? For example, for: def
Given: (function() { function Foo() { } $.extend(Foo.prototype, { bar: 'hasBeer' }); }) Is
Please tell me what will the call to given function return and how? The
Given a function, how to save it to an R script (.R)? Save works
Given a function y = f(A,X): unsigned long F(unsigned long A, unsigned long x)
Given a function, let's say atoi, how can I find the header file I
Given this function: > function Get-ArrayOfArrays() { (1,2),(3,4) | % { $_ } }
Given a function, I'd like to know whether it's a developer-defined function or a
I am given a function rand5() that generates, with a uniform distribution, a random
I have the following code (generates a quadratic function given the a, b, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.