Home > Archive > Unix Programming > July 2006 > Re: Possible to do the equivalent of dlopen with an executable
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: Possible to do the equivalent of dlopen with an executable
|
|
| Paul Pluzhnikov 2006-07-19, 7:01 pm |
| gazelle@xmission.xmission.com (Kenny McCormack) writes:
> Is it possible to do the equivalent of dlopen with an executable (i.e.,
> not shared lib) ?
On Linux, "dlopen(NULL, ...)" returns you the handle to the main
executable. This handle works fine for subsequent dlsym()s,
provided the exe exports the function you need in its dynamic symbol
table ("nm -D a.out").
> ISTR, that it was
> somehow possible to do the same thing (i.e., execute routines stored
> within) a regular executable as well.
Calling routines in the main executable from a DSO is easy, provided
that you know routine name at DSO compile time, and that the routine
is dynamically exported from the exe.
If the exe loads foo.so (as a plugin) and defines bar() as
dynamically exported symbol, then put this into foo.so:
int foo() {
return bar();
}
and foo.so`foo() will happily call a.out`bar()
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| Paul Pluzhnikov 2006-07-20, 8:00 am |
| gazelle@xmission.xmission.com (Kenny McCormack) writes:
> Note, incidentally, given that other posters have mentioned Windows and
> made comparisons thereto, that the actual origin of this problem is:
> I have an application which ships in both Windows and Solaris flavors
> and I am able to do this thing in the Windows version (it is supported
> there).
It is probably time to re-think your design.
> And, therefore, I'd like to be able to do the same in the Solaris version.
It is possible to do this on Solaris, but it will require so much
effort that you are unlikely to succeed on that path. And the effort
would have to be repeated on every other UNIX you'll want to port to.
All you have to do is "pretend" that your executable is the dynamic
linker (/lib/ld.so.1) and load (mmap) the executable yourself. Then
load all its direct dependencies, relocate, arrange for lazy symbol
resolution, etc. etc.
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| Alan Coopersmith 2006-07-21, 3:59 am |
| Paul Pluzhnikov <ppluzhnikov-nsp@charter.net> writes in comp.unix.solaris:
|It is possible to do this on Solaris, but it will require so much
|effort that you are unlikely to succeed on that path. And the effort
|would have to be repeated on every other UNIX you'll want to port to.
|
|All you have to do is "pretend" that your executable is the dynamic
|linker (/lib/ld.so.1) and load (mmap) the executable yourself. Then
|load all its direct dependencies, relocate, arrange for lazy symbol
|resolution, etc. etc.
XFree86 did this for the driver modules. Xorg dropped it and just uses
dlopen() and debugging is so much easier now. The code is out there
if you want to see how hard it is.
--
Alan Coopersmith * alanc@alum.calberkeley.org * Alan.Coopersmith@Sun.COM
http://blogs.sun.com/alanc/ * http://people.freedesktop.org/~alanc/
http://del.icio.us/alanc/ * http://www.csua.berkeley.edu/~alanc/
Working for, but definitely not speaking for, Sun Microsystems, Inc.
|
|
|
|
|