Address of function in a module
Dave Higton (1515) 3525 posts |
I’m writing a module in C. I need to pass, as an argument to a SWI in another module, the address of a function in mine. I’ve used a recipe that worked for an application, but this gives the address as something like 0×81F8. Which would be fine if it were an app, but the module might be anywhere, and the address needs to be computed at run time. How do I do that in C? |
Simon Willcocks (1499) 513 posts |
Subtract 0×8000 and add the address of the header. You might also be able to use inline assembler to do an ADR of the function. |
Stuart Swales (8827) 1357 posts |
Just cast the function to a But is the other module guaranteed to be an APCS-32 caller? You’ll surely want to go via a generic veneer otherwise when called your function won’t refer to the correct data? |
Dave Higton (1515) 3525 posts |
Thanks, both. The address I want to give is the address of the veneer. Pity the compiler doesn’t know what uintptr_t is… |
Simon Willcocks (1499) 513 posts |
Try “unsigned int”. |
Stuart Swales (8827) 1357 posts |
stdint.h is your friend |
Rick Murray (539) 13839 posts |
CMHG will make a header saying something like: extern void module_veneer_ticker(void); You can then call it like: r.r[0] = val; r.r[1] = (int)module_veneer_ticker; r.r[2] = (int)wsp; _kernel_swi(OS_CallAfter, &r, &r); You will then be expected to write the handler, which is called in response to the code at that address being entered. The name is the same, with “_handler” suffixed. _kernel_oserror *module_veneer_ticker_handler(_kernel_swi_regs *ir, void *pw) { ... } This code taken from my MIDI module, if you need a working example. |