3 ms·
I'd quite like to use bind() in C / nasm for my current project. I think you can use a similar approach - create an executable area on the heap that pushes an e
by mappu 14y ago
I'd quite like to use bind() in C / nasm for my current project. I think you can use a similar approach - create an executable area on the heap that pushes an extra parameter and jmps to the target function, then just return a function pointer to it.
EDIT: there might be a problem with arranging the stack parameters - if you use call on the target, then you're pushing a return address onto the stack, which messes up the arguments. But if you simply jmp, then neither the caller or callee know exactly how to clean up the stack when the function returns... hmm. I guess you could permanently reserve a register for this shimming but that's hardly an elegant solution.
- vedantk 14y agoI think the callee expects the return address in %rbp, and by System V convention will pull certain arguments from above %ebp on the stack. This might not help, but it's an interesting article on stack frames for all interested: http://eli.thegreenplace.net/2011/09/06/stack-frame-layout-on-x86-64/ http://eli.thegreenplace.net/2011/09/06/stack-frame-layout-o...
- mappu 14y agoLooks like fastcall, or any convention on x86_64. I think you can do bound functions if you do callee-cleanup, then your intermediate bound trampolines can just jmp and you don't end up with (too many) weird problems. But it means that the final return address ends up at the bottom of the stack, rather than at the top, like it would with a conventional push/call system. In any case it looks like cdecl is out of the question. Some further discussion in a slightly different context, if you're interested: http://stackoverflow.com/questions/11271848/implementing-bound-function-parameters-in-a-compiler/11272296#11272296 http://stackoverflow.com/questions/11271848/implementing-bou...