tags:

views:

283

answers:

8

I want to write a small utility to call arbitrary functions from a C shared library. User should be able to list all the exported functions similar to what objdump or nm does. I checked these utilities' source but they are intimidating. Couldn't find enough information on google, if dl library has this functionality either.

(Clarification edit: I don't want to just call a function which is known beforehand. I will appreciate an example fragment along your answer.)

+2  A: 

This might be near to what you're looking for: http://python.net/crew/theller/ctypes/

tonylo
With ctypes, you have to know what the number and types of arguments are ahead of time. This goes against the 'clarification edit'by the OP.See:http://python.net/crew/theller/ctypes/tutorial.html#specifying-the-required-argument-types-function-prototypes
Kevin
+2  A: 

Well, I'll speak a little bit about Windows. The C functions exported from DLLs do not contain information about the types, names, or number of arguments -- nor do I believe you can determine what the calling convention is for a given function.

For comparison, take a look at National Instrument's LabVIEW programming environment. You can import functions from DLLs, but you have to manually type in the type and names of the arguments before you use a given function. If this limitation is OK, please edit your question to reflect that.

I don't know what is possible with *nix environments.

EDIT: Regarding your clarification. If you don't know what the function is ahead of time, you're pretty screwed on Windows because in general you won't be able to determine what the number and types of arguments the functions take.

Kevin
I don't have to make a stdcall so I don't worry about the call stack.As far as I can remember, dll files still retains some information about exported functions like mangled names.
artificialidiot
Mangled names are retained and can tell you the argument information for C++ libraries (assuming you know how to un-mangle the names). But the OP specified "C". MSVC sometimes mangles C names for calling convention and the total size of the arguments in bytes -- but you still lack critical info.
Kevin
+1  A: 

You could try ParaDyn's SymtabAPI. It lets you grab all the symbols in a shared library (or executable) and look at their types, offset, etc. It's all wrapped up in a reasonably nice C++ interface and runs on a lot of platforms. It also provides support for binary rewriting, which you could potentially use to do what you're talking about at runtime.

Webpage is here:

http://www.paradyn.org/html/symtab2.1-features.html

Documentation is here:

http://ftp.cs.wisc.edu/paradyn/releases/release5.2/doc/symtabProgGuide.21.pdf

tgamblin
+1  A: 

A standard-ish API is the dlopen/dlsym API; AFAIK it's implemented by GNU libc on Linux and Mac OS X's standard C library (libSystem), and it might be implemented on Windows by MinGW or other compatibility packages.

millenomi
can anyone provide an example using this API?
artificialidiot
Try asking a new StackOverflow question or just googling. dlopen() is not available on Windows, but LoadLibrary() offers similar functionality.
Adam Mitz
A: 

The source code for nm and objdump are available. If you want to start from specification then ELF is what you want to look into.

/Allan

Allan Wind
A: 

Eek! You've touched on one of the very platform-dependent topics of programming. On windows, you have DLLs, on linux, you have ld.so, ld-linux.so, and mac os x's dyld.

duane
sorry you're not helping at all
artificialidiot
A: 

I've written something like this in Perl. On Win32 it runs dumpbin /exports, on POSIX it runs nm -gP. Then, since it's Perl, the results are interpreted using regular expressions: / _(\S+)@\d+/ for Win32 (stdcall functions) and /^(\S+) T/ for POSIX.

Adam Mitz
+1  A: 

Only sensible solution (without reinventing the wheel) seems to use libbfd. Downsides are its documentation is scarce and it is a bit bloated for my purposes.

artificialidiot