2015 Jul 16 9:54 AM
Is there a possibility to load a dynamic shared object/library from a file on the application server and load it's functions (i.e. a Foreign Function Interface) from ABAP?
I am aware that you can call kernel functions with the CALL statement, but perhaps there are functions in the kernel that support loading libraries and calling their functions?
2015 Jul 16 2:24 PM
I had posted this question on SO too: Can you load shared objects (libraries) and call their functions (FFI) from ABAP? - Stack Overflow and received some helpful input from user mjturner, who pointed me to the dlopen() function, which is part of the standard C library (I think).
Anyway, so looking at an nm dump of the disp+work binary, you see the following symbols, which look like there are wrapper functions for dlopen():
0000000001bf3698 T dlopenU
0000000001beaf80 T dlopenU16
0000000001f486bc T SAP::datkm::dlopen(unsigned short const*, int)
0000000001f492c2 T SAP::datkm::dlopen2(char const*)
Now you could go into GDB and set breakpoints on these functions. The trick then is to find something that would likely cause these to be called, and see where and how they are called, to figure out how you could call them from ABAP (indirectly).
2015 Jul 16 2:37 PM
Interesting discovery. I guess you could trawl all of the ABAP CALL
statements and see what kernel functions they call and then work backwards to see which has a signature that matches those functions you've found.
I'm still of the opinion that the easier route is going to be to write an RFC server in C and use that to open the library in question and call exposed functions dynamically. It'll be a bit of work (particularly if you need to support Windows as well as Unix), but a very interesting exercise. No, I'm not volunteering to write it. At least not this week...
2015 Jul 16 2:55 PM
Me neither 🙂 We are actually in the middle of a go-live here, and I am busy writing this in-between mountains of queries.
You are probably right that your RFC server idea is a more practical route, because it won't rely on using any undocumented features.
I could write the RFC server in my nwrfc library and use Ruby-FFI (which I am already using to wrap the NW RFC SDK) to load a library (and it's cross-platform!).
But digging around in undocumented muck can produce interesting things 🙂
2015 Jul 16 3:18 PM
Oh yes, another thing is that the CALL statement in ABAP makes use of named parameters, which I guess means the functions must be declared in a certain way, such as requiring a very specific signature, perhaps?
2015 Jul 16 3:27 PM
Sounds like a good idea.
Now that I think of it, there's nothing that would require C. As long as you don't need to do things like determine the symbols exported by a library (which would require poking around in the in-memory ELF data structures, on Linux at least), you should able to do everything in Ruby.
2015 Jul 16 3:36 PM
Yes, I believe the names used in the ABAP CALL
are referenced in the kernel function by the same name (you can't just use arbitrary names). The parameter handling is all done by the kernel API.
2015 Jul 17 8:29 AM
This thread points to a SAP Help page which gives more detail on implementing kernel methods: http://help.sap.com/abapdocu_70/en/ABENKERNEL_METHODS.htm. It describes in some detail how to code and register a kernel method, but of course it is like being allowed to see all the delicious pastries in a bakery window, only to have the shutter drawn in front of us.
Pity that they don't give customers the ability to write their own kernel modules. At least not that I have ever heard of.
It would be quite nice if one could build FFI functionality directly into ABAP. Imagine the nice things you could do with it: embed some scripting language or sqlite or easy network or file access or offloading some data crunching to another tool. The possibilities are vast.