Article View: gnu.g++.help
Article #190dynamic linking/loading with g++ (2.2.2) and SunOS4.1.2
From: Tim Wagner
Date: Tue, 18 Aug 1992 02:30
Date: Tue, 18 Aug 1992 02:30
59 lines
2599 bytes
2599 bytes
I'm trying to use the sunOS shared library system to get dynamic
linking to work with g++. The basic operation works fine, but
static con(de)structors need to be called in the loaded code to
no surprise. My attempt was to use the code from crt0, like this:
typedef void (*func_ptr)();
extern func_ptr __CTOR_LIST__[];
func_ptr *p;
for (p = __CTOR_LIST__ + 1; *p;) {
(*p++) ();
}
This should step through the array of function pointers placed in
__CTOR_LIST__ + 4 by the `collect2' program.
The problem is that the __CTOR_LIST__ I bind to is the one from
the *main* executable, not the dynamically loaded image. This is strange,
given that the shared library shows only a defined __CTOR_LIST__, not an
undefined one, so presumably no further binding of that symbol would
occur. I have verified that it does using gdb to look at the address
and contents of __CTOR_LIST__ in the loaded image *after* it has been
dynamically loaded. Running `nm' on the shared library shows only the
defined symbol.
Here's the makefile, which describes how I build the main program
and the part to be loaded:
prog: main.o
g++-2.2.2 -o prog main.o -ldl -lg++-2_1
main.o: main.cc
g++-2.2.2 -c -o main.o main.cc
lib.so.1.1: lib.o
gcc-lib/sun4/2.2.2/ld -o lib.so.1.1 lib.o
lib.o: lib.cc
g++-2.2.2 -fpic -c -o lib.o lib.cc
(The main program loads the file "lib.so.1.1" via the `dl' library
functions.)
So my questions are:
(1) Why is this happening?
(2) Has anyone gotten dynamic linking to work in a similar fashion?
(3) What else lurks in this pool to bite me?
I should mention that if I call the magic functions (the compiler-generated
thunks of the form _GLOBAL$I$name and _GLOBAL$D$name) manually, all is
well and the loaded code's static con(de)structors are fine. But a better
method than that should be possible.
Please respond via e-mail; I will post any solutions found if people
register interest in the problem.
-Tim Wagner
UC Berkeley CS Department
508-9 Evans
twagner@CS.Berkeley.EDU
Technical Details
Message-ID:
<16pnbiINNs8h@agate.berkeley.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!usc!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!ames!agate!baobab.CS.Berkeley.EDU!twagnerShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!usc!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!ames!agate!baobab.CS.Berkeley.EDU!twagner From: twagner@baobab.CS.Berkeley.EDU (Tim Wagner) Newsgroups: gnu.g++.help Subject: dynamic linking/loading with g++ (2.2.2) and SunOS4.1.2 Message-ID: <16pnbiINNs8h@agate.berkeley.edu> Date: 18 Aug 92 02:30:10 GMT Distribution: world Organization: UC Berkeley, University of California Berkeley Lines: 59 NNTP-Posting-Host: baobab.cs.berkeley.edu