The so library has several export functions. Let's say one of them is test(). I could see test() in this so's export functions list. Now I'd like to only call the test() function in command line. How should I get it?
The closest you can find, is Perl. You can use PDL::CallExt
callext() loads in an so using Perl's dynamic loader, calls the named function and passes a list of piddle arguments to it.
You can even use Perl to compile C into a shared object with appropriate flags, so you can create native library on the fly.
Related
The short version:
I'm trying to use a function in a shared library that hasn't been declared in a header file. Is it possible? If so, how?
The long version:
I'm trying to use GNU readline-7.0 as a shared library. Unfortunately it will echo input by default unless you turn it off with an api call. Sadly this api call wasn't declared in their header, because someone forgot. see details here Note: I can't update to a newer readline, so I'm stuck with this library. How do I use this undeclared function?
As noted in the linked bug, there is no external declaration in the header file even though the function exists in the implementation.
The link also shows the fix with the added declaration:
extern int rl_tty_set_echoing PARAMS((int));
So you can add this in your code right after #include <readline.h>
The sort answer is yes!!!
Just use the library -ldl with function dlopen(3), etc. This library allows you to load a shared object and search for identifiers that you know nothing about. Of course, the static typing nature of C forces to declare the interface of those functions with a prototype, so once you get a (void *) to the object you can cast it to something like int (*t)() and jump to it with a t(); call.
In the manual page you will get the details, dlopen allows you to search a shared object for symbols passed as strings. It returns pointers to the places those identifiers have been loaded in memory. You can even do this with your own program.
On the other hand, this is a feature of ELF shared loaded modules. If you are running a.out executables or otherwise, it is now waranteed that you'll have a dlopen(3) library for it, or that it will work that way.
I'd like to write some C code (okay if it only works on Linux) to dynamically load a new shared library, and then invoke a method from it (to be determined at runtime). It seems this is already possible because java can load native libraries dynamically, and then invoke methods from them.
For example, I'd like to do something like:
int main() {
libinfo_t * lib_details = load_shared_library("libfoo.so");
run_method(lib_details, "bar", 7);
}
This would invoke the method 'bar' with argument 7 (bar is a method compiled into libfoo.so).
Use case details:
I'd like to compile a binary that loads all the shared libraries in a directory, and runs some method from each, in the memory context of the original program. I'd like to be able to quickly enable or disable a shared library by adding/removing it from a directory.
Proof of concept:
It seems this should be possible, based on the way java manages to link with jni code dynamically. You can use System.load(), and load the library of your choice. Coupled with compiling from memory, it seems it would allow you to run an arbitrary function from an arbitrary library. http://www.java2s.com/Code/Java/JDK-6/CompilingfromMemory.htm
Things I've tried:
I've looked at the manpage for 'uselib', which seems useful, but I'm not sure what to do with the library once I've loaded it.
A bit of googling returned http://dyncall.org/, but this isn't exactly what I need -- this project still requires a function pointer to make the function call.
I'd be grateful for any pointer on where to look next, even without a concrete answer. Thanks!
Linux has a very complete API for this. It's the dlopen(3) API.
First, you call dlopen with a filename to get a shared library handle:
void* lib = dlopen("./lib.so");
Than, to get a function pointer for a function in this library:
int (*func)() = dlsym(lib, "thing");
Use this pointer as you please.
Finally, when you're done:
dlclose(lib)
Note: Remember to do error checking!
I'd like to write a wrapper around pthread_create in a dynamic library.
I use ld -wrap to link my libwrap.so file.
When I link my application with "-Wl,--wrap,pthread_create -lwrap" it works fine, my function is called instead of the real one.
If I use only "-lwrap", only the functions of libwrap.so use my function, the ones in the application use the real one.
Is there any way to make the application use the wrapper functions of the dynamic library without linking the application with the "-Wl,--wrap,pthread_create" ?
Thanks,
Fred
Is there any way to make the application use the wrapper functions of the dynamic library without linking the application with the "-Wl,--wrap,pthread_create"
Yes, but wrapper functions are not the right way to go about it.
What you want is an interposer library. Read about library interposers here.
I want to be able to use a char *function_name = "foo" to call foo() from a C program. The foo() routine would be in a shared library. I don't have access to any dlopen, etc, just the load step for a regular executable. Is there any way to resolve this name and load it from the shared library?
No. You either use dlopen and dlsym, or you make your own array of function names and function pointers and look it up that way.
You can't do this unless you have access to the function names and load addresses. You can do this using LoadLibrary() and GetProcAddress() in Windows.
In your case, I don't know exactly what you have available (you didn't mention your platform, compiler, etc.) or if this is feasible. I strongly suspect, however, that it is not.
The common method to call functions by name is to use a table of names and function pointers. A caveat to this technique is that the functions must all have the same signature.
If you know the names functions but the function signatures differ, you will have to use and if-then-else ladder.
c and i use it to generate x.so shared library
in x.c i want to use few functions that are in the main module, (dir containing main files and exe), kind of recursive dependeny.
is there a way to do this (without copying those functions in x.c) ?
i read about -rdynamic , but could not get it fully.
when i compile i get 'somefunc' undeclared. (somefunc is in main module, i did extern somefunx in x.c but did not work)
please let me know
thanks
You could define the affected methods in your shared library to take the call back function pointer arguments, and then at call time pass the main module's functions as arguments. E.g.
// Library
void dosomething (int arg, void (*callback)(void)) { ... }
// Main module
void called_from_lib(void) { ... }
dosomething(10, called_from_lib);
This looks like unix. There is a function, dlopen(), that lets you dynamically call a function in a library - without referencing it at compile time and without linking it into the program. dlopen() is POSIX, and so should be on any modern unix box.
Example here:
http://www.dwheeler.com/program-library/Program-Library-HOWTO/x172.html
There is also LD_LIBRARY_PATH. This environment variable lets you use the same code, but allows you to substitute in a library that was not there at compile time. This is not exactly what you are asking, but it can be made to do something along the lines of using adhoc shared libraries without resorting to dlopen. Some systems like HPUX also support SHLIB_PATH which does the same thing.