niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » *nix » Solaris
Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ?
Post new topic   Reply to topic Page 1 of 1 [15 Posts] View previous topic :: View next topic
Author Message
Paul Pluzhnikov
*nix forums Guru


Joined: 25 Mar 2005
Posts: 512

PostPosted: Fri Jul 21, 2006 1:27 pm    Post subject: Re: Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ? Reply with quote

Alan Coopersmith <alanc@alum.calberkeley.org> writes:

Quote:
Paul Pluzhnikov <ppluzhnikov-nsp@charter.net> writes in comp.unix.solaris:
....
|All you have to do is "pretend" that your executable is the dynamic
|linker (/lib/ld.so.1) and load (mmap) the executable yourself...
....
XFree86 did this for the driver modules.

Interesting. I never looked at that code.

Did it actually implement complete ld.so equivalent, or did it
"cheat" by resolving all references at driver load time (equivalent
of LD_BIND_NOW)? Were the modules in ELF, or some other more
convenient and simpler format?

Thanks,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Back to top
Alan Coopersmith
*nix forums Guru


Joined: 20 Feb 2005
Posts: 398

PostPosted: Fri Jul 21, 2006 5:42 am    Post subject: Re: Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ? Reply with quote

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.
Back to top
Paul Pluzhnikov
*nix forums Guru


Joined: 25 Mar 2005
Posts: 512

PostPosted: Thu Jul 20, 2006 1:40 pm    Post subject: Re: Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ? Reply with quote

gazelle@xmission.xmission.com (Kenny McCormack) writes:

Quote:
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.

Quote:
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.
Back to top
Kenny McCormack
*nix forums Guru


Joined: 24 Mar 2005
Posts: 657

PostPosted: Thu Jul 20, 2006 1:31 pm    Post subject: Re: Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ? Reply with quote

In article <44bf707f$0$31650$e4fe514c@news.xs4all.nl>,
Casper H.S. Dik <Casper.Dik@Sun.COM> wrote:
Quote:
Paul Pluzhnikov <ppluzhnikov-nsp@charter.net> writes:

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").

Since Linux is generally a clone of the Solaris interface but than
usually some revs behind, the same mechanism also works for Solaris:

Yes, but as you (I believe it was you) correctly surmised a response or
two back, the issue is that I want to have, essentially, 2 executables
running at once - i.e., a program that I write (in C, that compiles to
an ELF executable) that calls routines in another ELF executable.

I'm well aware of the dlopen(NULL) thing, but it doesn't apply here.

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).

And, therefore, I'd like to be able to do the same in the Solaris version.
Back to top
Paul Pluzhnikov
*nix forums Guru


Joined: 25 Mar 2005
Posts: 512

PostPosted: Thu Jul 20, 2006 1:17 pm    Post subject: Re: Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ? Reply with quote

davids@webmaster.com writes:

Quote:
On Windows, DLLs and executables are almost identical....

If you think about it, there's really no special reason they need to be
different. They both need to be mapped and executed. They both contain
functions that need to be exported (otherwise, how could a DLL call a
function in the executable that called it?).

A DLL could easily call non-exported function from the main
executable -- all DLL needs is its address (which is often passed
as a "callback" parameter).

Quote:
It just so happens that on most UNIXes, they are very different.

With recent Linux "-pie" executables and "prelinked" DSOs, the
difference between ET_EXEC and ET_DYN is almost nil.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Back to top
Casper H.S. Dik
*nix forums Guru


Joined: 20 Feb 2005
Posts: 1634

PostPosted: Thu Jul 20, 2006 12:01 pm    Post subject: Re: Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ? Reply with quote

Paul Pluzhnikov <ppluzhnikov-nsp@charter.net> writes:

Quote:
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").

Since Linux is generally a clone of the Solaris interface but than
usually some revs behind, the same mechanism also works for Solaris:

THere are several options in Solaris:

dlopen(NULL):

If the value of pathname is 0, dlopen() provides a handle on
a set of global symbol objects. These objects consist of the
original program image file, any dependencies loaded at pro-
gram startup, and any objects loaded using dlopen() with the
RTLD_GLOBAL flag. Because the latter set of objects can
change during process execution, the set identified by han-
dle can also change dynamically.


or fetch dlsym using a special object:

RTLD_DEFAULT
RTLD_NEXT
RTLD_SELF


Casper
Back to top
David Schwartz
*nix forums Guru


Joined: 26 Feb 2005
Posts: 914

PostPosted: Thu Jul 20, 2006 10:25 am    Post subject: Re: Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ? Reply with quote

Michael Vilain wrote:

Quote:
I've never heard of this capability. AFAIK, you can only link to shared
libraries, not executables. Time for a rethink on what you're trying to
do.

On Windows, DLLs and executables are almost identical. Executables can
even export functions that can be called the same way functions in a
DLL are called -- it's just almost never done.

If you think about it, there's really no special reason they need to be
different. They both need to be mapped and executed. They both contain
functions that need to be exported (otherwise, how could a DLL call a
function in the executable that called it?). They both need
initialization code to initialize their memory space and global objects
when they're loaded. And so on.

It just so happens that on most UNIXes, they are very different.

DS
Back to top
Barry Margolin
*nix forums Guru


Joined: 24 Feb 2005
Posts: 323

PostPosted: Thu Jul 20, 2006 12:42 am    Post subject: Re: Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ? Reply with quote

In article <87mzb5mq8z.fsf@thalassa.informatimago.com>,
Pascal Bourguignon <pjb@informatimago.com> wrote:

Quote:
Michael Vilain <vilain@spamcop.net> writes:

In article <e9lp08$rig$1@news.xmission.com>,
gazelle@xmission.xmission.com (Kenny McCormack) wrote:

Is it possible to do the equivalent of dlopen with an executable (i.e.,
not shared lib) ?

When I try to dlopen() an (dynamically linked) executable program, I get
a NULL return and the following error from dlerror():

ld.so.1: ./myprog: elf error: ../path/something: not a shared or
relocatable object: Error 0

which is, of course, perfectly reasonable. But, ISTR, that it was
somehow possible to do the same thing (i.e., execute routines stored
within) a regular executable as well. Am I remembering wrong?

I've never heard of this capability. AFAIK, you can only link to shared
libraries, not executables. Time for a rethink on what you're trying to
do.


Well, if gdb can do it:

gdb doesn't do it by linking the executable into its process. It runs
the executable in a separate process and controls/monitors it using
ptrace() (or something equivalent with /proc).

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Back to top
Paul Pluzhnikov
*nix forums Guru


Joined: 25 Mar 2005
Posts: 512

PostPosted: Thu Jul 20, 2006 12:39 am    Post subject: Re: Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ? Reply with quote

gazelle@xmission.xmission.com (Kenny McCormack) writes:

Quote:
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").

Quote:
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.
Back to top
Kenny McCormack
*nix forums Guru


Joined: 24 Mar 2005
Posts: 657

PostPosted: Wed Jul 19, 2006 11:35 pm    Post subject: Re: Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ? Reply with quote

In article <87r70hmsb7.fsf@thalassa.informatimago.com>,
Pascal Bourguignon <pjb@informatimago.com> wrote:
Quote:
gazelle@xmission.xmission.com (Kenny McCormack) writes:

Is it possible to do the equivalent of dlopen with an executable (i.e.,
not shared lib) ?

When I try to dlopen() an (dynamically linked) executable program, I get
a NULL return and the following error from dlerror():

ld.so.1: ./myprog: elf error: ../path/something: not a shared or
relocatable object: Error 0

which is, of course, perfectly reasonable. But, ISTR, that it was
somehow possible to do the same thing (i.e., execute routines stored
within) a regular executable as well. Am I remembering wrong?

It's more complicated, but with enough work, it should be doable.
Firs tyou need to know what is the format of the excutable file
(a.out, elf, cof, etc). Then locate a library allowing you to load
it, find symbols, relocate it, dynamically link it with the libraries
it needs, etc, and eventually you may be able to call some function
from the executable.

But really, it would be easier to type make in the source directory of
the program, and then to build a dynamic library from the .o files
remaining...

The problem is, of course, that the executable does not come with
source. Therefore, cannot re-compile, cannot "re-think what I am trying
to do".

And, FYI, the platform in question is Solaris.

I suppose I need to do some googling; just wanted to check to make sure
it wasn't something obvious that I was overlooking.
Back to top
Casper H.S. Dik
*nix forums Guru


Joined: 20 Feb 2005
Posts: 1634

PostPosted: Wed Jul 19, 2006 6:25 pm    Post subject: Re: Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ? Reply with quote

gazelle@xmission.xmission.com (Kenny McCormack) writes:

Quote:
Is it possible to do the equivalent of dlopen with an executable (i.e.,
not shared lib) ?

When I try to dlopen() an (dynamically linked) executable program, I get
a NULL return and the following error from dlerror():

ld.so.1: ./myprog: elf error: ../path/something: not a shared or
relocatable object: Error 0

which is, of course, perfectly reasonable. But, ISTR, that it was
somehow possible to do the same thing (i.e., execute routines stored
within) a regular executable as well. Am I remembering wrong?



THe typical problem is that an executable is often not relocatable and
that it would be mapped on top of the current executable because of
overlapping address ranges; all processes are mapped at the same start
address on most systems.

So ld.so complains because an executable cannot be relocated.

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
Back to top
Pascal Bourguignon
*nix forums addict


Joined: 08 Mar 2005
Posts: 62

PostPosted: Wed Jul 19, 2006 5:57 pm    Post subject: Re: Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ? Reply with quote

Michael Vilain <vilain@spamcop.net> writes:

Quote:
In article <e9lp08$rig$1@news.xmission.com>,
gazelle@xmission.xmission.com (Kenny McCormack) wrote:

Is it possible to do the equivalent of dlopen with an executable (i.e.,
not shared lib) ?

When I try to dlopen() an (dynamically linked) executable program, I get
a NULL return and the following error from dlerror():

ld.so.1: ./myprog: elf error: ../path/something: not a shared or
relocatable object: Error 0

which is, of course, perfectly reasonable. But, ISTR, that it was
somehow possible to do the same thing (i.e., execute routines stored
within) a regular executable as well. Am I remembering wrong?

I've never heard of this capability. AFAIK, you can only link to shared
libraries, not executables. Time for a rethink on what you're trying to
do.


Well, if gdb can do it:


[pjb@thalassa tmp]$ gdb ./a
GNU gdb 5.3
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i586-suse-linux"...
(gdb) break main
Breakpoint 1 at 0x804834b
(gdb) run
Starting program: /tmp/a

Breakpoint 1, 0x0804834b in main ()
(gdb) print fact(5)
$1 = 120
(gdb) shell cat a.c
int fact(int x){return((1==x)?(1)Sadx*fact(x-1)));}
int main(void){return(fact(1));}
(gdb) quit
A debugging session is active.
Do you still want to close the debugger?(y or n) y
[pjb@thalassa tmp]$


--
__Pascal Bourguignon__ http://www.informatimago.com/
You never feed me.
Perhaps I'll sleep on your face.
That will sure show you.
Back to top
Michael Vilain
*nix forums Guru


Joined: 21 Feb 2005
Posts: 565

PostPosted: Wed Jul 19, 2006 5:42 pm    Post subject: Re: Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ? Reply with quote

In article <e9lp08$rig$1@news.xmission.com>,
gazelle@xmission.xmission.com (Kenny McCormack) wrote:

Quote:
Is it possible to do the equivalent of dlopen with an executable (i.e.,
not shared lib) ?

When I try to dlopen() an (dynamically linked) executable program, I get
a NULL return and the following error from dlerror():

ld.so.1: ./myprog: elf error: ../path/something: not a shared or
relocatable object: Error 0

which is, of course, perfectly reasonable. But, ISTR, that it was
somehow possible to do the same thing (i.e., execute routines stored
within) a regular executable as well. Am I remembering wrong?

I've never heard of this capability. AFAIK, you can only link to shared
libraries, not executables. Time for a rethink on what you're trying to
do.

--
DeeDee, don't press that button! DeeDee! NO! Dee...
Back to top
Pascal Bourguignon
*nix forums addict


Joined: 08 Mar 2005
Posts: 62

PostPosted: Wed Jul 19, 2006 5:13 pm    Post subject: Re: Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ? Reply with quote

gazelle@xmission.xmission.com (Kenny McCormack) writes:

Quote:
Is it possible to do the equivalent of dlopen with an executable (i.e.,
not shared lib) ?

When I try to dlopen() an (dynamically linked) executable program, I get
a NULL return and the following error from dlerror():

ld.so.1: ./myprog: elf error: ../path/something: not a shared or
relocatable object: Error 0

which is, of course, perfectly reasonable. But, ISTR, that it was
somehow possible to do the same thing (i.e., execute routines stored
within) a regular executable as well. Am I remembering wrong?

It's more complicated, but with enough work, it should be doable.
Firs tyou need to know what is the format of the excutable file
(a.out, elf, cof, etc). Then locate a library allowing you to load
it, find symbols, relocate it, dynamically link it with the libraries
it needs, etc, and eventually you may be able to call some function
from the executable.

But really, it would be easier to type make in the source directory of
the program, and then to build a dynamic library from the .o files
remaining...

--
__Pascal Bourguignon__ http://www.informatimago.com/

"You can tell the Lisp programmers. They have pockets full of punch
cards with close parentheses on them." --> http://tinyurl.com/8ubpf
Back to top
Kenny McCormack
*nix forums Guru


Joined: 24 Mar 2005
Posts: 657

PostPosted: Wed Jul 19, 2006 5:09 pm    Post subject: Possible to do the equivalent of dlopen with an executable (i.e., not shared lib) ? Reply with quote

Is it possible to do the equivalent of dlopen with an executable (i.e.,
not shared lib) ?

When I try to dlopen() an (dynamically linked) executable program, I get
a NULL return and the following error from dlerror():

ld.so.1: ./myprog: elf error: ../path/something: not a shared or
relocatable object: Error 0

which is, of course, perfectly reasonable. But, ISTR, that it was
somehow possible to do the same thing (i.e., execute routines stored
within) a regular executable as well. Am I remembering wrong?
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [15 Posts] View previous topic :: View next topic
The time now is Fri Nov 21, 2008 8:48 pm | All times are GMT
navigation Forum index » *nix » Solaris
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts database Share Memory Limit (2 GB ) in a Instance is tota... sadanjan@gmail.com IBM DB2 0 Fri Jul 21, 2006 12:57 pm
No new posts equivalent of MAIL>SET FORWARD/USER=ME YOU on unix VAXman-@SendSpamHere.ORG VMS 4 Thu Jul 20, 2006 7:56 pm
No new posts ImportError: libclntsh.so.10.1: cannot open shared object... gmax2006 python 2 Thu Jul 20, 2006 7:05 pm
No new posts equivalent to .qmail for outgoing? Bill Gradwohl Qmail 1 Thu Jul 20, 2006 3:37 pm
No new posts Help! Shared memory is getting deleted after process is k... Anuradha apps 3 Thu Jul 20, 2006 8:42 am

Mortgage Calculator | Credit Cards UK | Free Wordpress Blogs Hosting | Xbox Mod Chip | Mortgages
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.3238s ][ Queries: 20 (0.1628s) ][ GZIP on - Debug on ]