473,385 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with dlopen

Hello,

I have a program that makes use of external plugins, implemented as
shared libraries loaded at runtime through a call to dlopen(). The
libraries export symbols which the main program collects through a call
to dlsym().

The problem is that the libraries refer to certain symbols from the main
program. But the linker at runtime fails to resolve these "undefined
symbols" in the shared library.

I think I may have misunderstood something in how dlopen() works - can
anyone give me some advice?

Thanks!
Apr 7 '08 #1
10 3560
On 7 Apr 2008 at 17:13, Unknown Soldier wrote:
Hello,

I have a program that makes use of external plugins, implemented as
shared libraries loaded at runtime through a call to dlopen(). The
libraries export symbols which the main program collects through a call
to dlsym().

The problem is that the libraries refer to certain symbols from the main
program. But the linker at runtime fails to resolve these "undefined
symbols" in the shared library.

I think I may have misunderstood something in how dlopen() works - can
anyone give me some advice?
Did you link the main program with -rdynamic? If not, only the symbols
the external library knows it needs at link-time will be exported.

Apr 7 '08 #2
In article <ft*********@aioe.org>, Unknown Soldier <no****@nospam.comwrote:
>I have a program that makes use of external plugins, implemented as
shared libraries loaded at runtime through a call to dlopen().
dlopen() is not part of standard C, and the exact operations of
dlopen() depend upon your operating system. Please contact a
development group that deals with your particular operating system.

If I recall correctly from my scanning, on -some- systems,
"backwards" referrals from a shared library can only be implemented
by having the shared library itself dlopen the "earlier" object
(e.g., in your case, the library might have to dlopen() the main
executable.) In other systems, a similar effect might be in place
but rather than having to "manually" dlopen() the earlier objects,
just having the shared library refer to the object in its headers
is enough to trigger a transitive dlopen(). And then there are other
systems that these restrictions don't apply to and it should Just Work,
and other systems on which it should Just Work but only if the
object backwards-referenced was defined as a global object. Different
systems, different rules, so you need to check the rules for -your-
system.
--
"[i]t lacks context, and may or may not make sense."
-- Walter J. Phillips
Apr 7 '08 #3

"Unknown Soldier" <no****@nospam.comwrote in message
I have a program that makes use of external plugins, implemented as shared
libraries loaded at runtime through a call to dlopen(). The libraries
export symbols which the main program collects through a call to dlsym().
It sounds like someone is trying to implement some type of dynamic linking
on top of C. Regular C programs link symbols at link time, they don't call
functions to retrieve lists of symbols.
>
The problem is that the libraries refer to certain symbols from the main
program. But the linker at runtime fails to resolve these "undefined
symbols" in the shared library.
There are two main possibilities. You might need to link a third library,
or you might need to include functions / symbols in your user program for
dlopen to link in.
>
I think I may have misunderstood something in how dlopen() works - can
anyone give me some advice?
Unfortunately although I can help in general terms I don't know exactly how
this specific system works. You might want to try a platform-specific
newsgroup.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Apr 7 '08 #4
In article <ft*********@aioe.org>, Unknown Soldier <no****@nospam.comwrote:
>I think I may have misunderstood something in how dlopen() works - can
anyone give me some advice?
It smells like POSIX to me; if true, that means the people in
comp.unix.programmer probably could.
dave

--
Dave Vandervies dj3vande at eskimo dot com
If this confuses you, I would recommend staying away from the shipbuilding
trade, where they measure weight in terms of volume.
--Ben Ketcham in comp.lang.c
Apr 7 '08 #5
That did the trick, thanks a lot for the quick answer!
Antoninus Twink wrote:
On 7 Apr 2008 at 17:13, Unknown Soldier wrote:
>>Hello,

I have a program that makes use of external plugins, implemented as
shared libraries loaded at runtime through a call to dlopen(). The
libraries export symbols which the main program collects through a call
to dlsym().

The problem is that the libraries refer to certain symbols from the main
program. But the linker at runtime fails to resolve these "undefined
symbols" in the shared library.

I think I may have misunderstood something in how dlopen() works - can
anyone give me some advice?


Did you link the main program with -rdynamic? If not, only the symbols
the external library knows it needs at link-time will be exported.
Apr 7 '08 #6
Unknown Soldier wrote:
Hello,

I have a program that makes use of external plugins, implemented as
shared libraries loaded at runtime through a call to dlopen(). The
libraries export symbols which the main program collects through a call
to dlsym().

The problem is that the libraries refer to certain symbols from the main
program. But the linker at runtime fails to resolve these "undefined
symbols" in the shared library.

I think I may have misunderstood something in how dlopen() works - can
anyone give me some advice?

Thanks!
Do you have some sample code to demonstrate this technique, this sounds
interesting.

Fei
Apr 7 '08 #7
On 7 Apr 2008 at 21:34, Fei Liu wrote:
Unknown Soldier wrote:
>Hello,

I have a program that makes use of external plugins, implemented as
shared libraries loaded at runtime through a call to dlopen(). The
libraries export symbols which the main program collects through a call
to dlsym().

The problem is that the libraries refer to certain symbols from the main
program. But the linker at runtime fails to resolve these "undefined
symbols" in the shared library.

I think I may have misunderstood something in how dlopen() works - can
anyone give me some advice?

Thanks!

Do you have some sample code to demonstrate this technique, this sounds
interesting.
Here's a simple example with the shared library code referencing a
symbol from the main executable:
/* prog.c */

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

int main(void)
{
void (*f)(void);
void *handle;

handle = dlopen ("./shared.so", RTLD_NOW);
if (!handle)
abort();

f = dlsym (handle, "f");
if (!f)
abort();
f();

if(dlclose(handle))
abort();
return 0;
}

void g(void)
{
puts("Calling g...");
}

/* shared.c */

#include <stdio.h>

extern void g(void);

void f(void)
{
puts("Calling f...");
g();
}


$ make prog LDFLAGS=-rdynamic LDLIBS=-ldl
cc -rdynamic prog.c -ldl -o prog
$ make shared.o
cc -c -o shared.o shared.c
$ ld -shared -o shared.so shared.o
$ ./prog
Calling f...
Calling g...

Apr 7 '08 #8
Antoninus Twink <no****@nospam.invalidwrites:
[...]
Here's a simple example with the shared library code referencing a
symbol from the main executable:
[...]

Why don't you post this in comp.unix.programmer?

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 7 '08 #9
Keith Thompson <ks***@mib.orgwrites:
Antoninus Twink <no****@nospam.invalidwrites:
[...]
>Here's a simple example with the shared library code referencing a
symbol from the main executable:
[...]

Why don't you post this in comp.unix.programmer?
Because it was requested here and it's in C.

Why do you ask? It seems fairly obvious. You use Gnus. If you are not
interested then kill the thread. Further discussions can move to the
other NG when it starts to deepen.
Apr 7 '08 #10
Antoninus Twink wrote:
On 7 Apr 2008 at 21:34, Fei Liu wrote:
>Unknown Soldier wrote:
>>Hello,

I have a program that makes use of external plugins, implemented as
shared libraries loaded at runtime through a call to dlopen(). The
libraries export symbols which the main program collects through a call
to dlsym().

The problem is that the libraries refer to certain symbols from the main
program. But the linker at runtime fails to resolve these "undefined
symbols" in the shared library.

I think I may have misunderstood something in how dlopen() works - can
anyone give me some advice?

Thanks!
Do you have some sample code to demonstrate this technique, this sounds
interesting.

Here's a simple example with the shared library code referencing a
symbol from the main executable:
/* prog.c */

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

int main(void)
{
void (*f)(void);
void *handle;

handle = dlopen ("./shared.so", RTLD_NOW);
if (!handle)
abort();

f = dlsym (handle, "f");
if (!f)
abort();
f();

if(dlclose(handle))
abort();
return 0;
}

void g(void)
{
puts("Calling g...");
}

/* shared.c */

#include <stdio.h>

extern void g(void);

void f(void)
{
puts("Calling f...");
g();
}


$ make prog LDFLAGS=-rdynamic LDLIBS=-ldl
cc -rdynamic prog.c -ldl -o prog
$ make shared.o
cc -c -o shared.o shared.c
$ ld -shared -o shared.so shared.o
$ ./prog
Calling f...
Calling g...
Very interesting, thanks a lot!

Fei
Apr 8 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave Harrison | last post by:
Ok before I start, please dont mail me telling me to use a more recent version of Python, I _would_ use 2.2.x but due to an existing framework all based on using 2.1.1 I have been specifically told...
2
by: Tero Pihlajakoski | last post by:
I'm trying to run some python scripts from a proggy of mine. Most of the time it works great, but I seem to be having problems with some modules. Here's what I get when I try importing math...
1
by: Attila.Rajmund.Nohl | last post by:
Hello! I'm using KAI C++ Compiler (KCC) Version 4.0d on Sparc Solaris 8 with Sun WorkShop 6 update 2 backend (KCC compiles C++ code to C than uses the Sun compiler to produce machine...
3
by: KeithO | last post by:
I am having problems calling dynamic_cast<> on a pointer returned by a dynamically loaded library. The problem seems to be related to the fact that I dynamically load the library because if I link...
2
by: Pablo Santa Cruz | last post by:
Hello all, I am having runtime problems with a library (shared object) I am making in Linux: Distro: RedHat 9.0 (RH90) Compiler: gnu gcc 3.2.2 I am linking the library correctly (I guess)...
2
by: Dave | last post by:
Hi all... I wrote a simple program to test the dlopen()...\ #include <dlfcn.h> int main(int argc, char **argv) { void *handle; double (*cosine)(double); char *error;
5
by: Michael Rudolph | last post by:
Hi newsgroup, I have an issue with the configuration of a DB2 federated database (WebSphere Information Integrator) in conjunction with the relational wrapper for Oracle on AIX. DB2 seems to not...
2
by: jiang.haiyun | last post by:
Hi, I am having some serious problems with PyQT4, when i run pyqt script, I always get 'Segmentation fault'. the script is simple: ====================== %less qttest.py from PyQt4 import...
6
by: tvnaidu | last post by:
opening shared lib with dlopen, gets success, but dlsym returns an error saying undefined symbol, also passing "-rdynamic" during linking this shared lib, any idea?. dlopen success, dlsym gets error,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.