Has anyone encounter the same problem?
Does anyone know what is special in Redhat gcc3.2.
Any help will be greatly appreciated.
-Shen
P.S. I know the case is kind of complicated and I hope I mention it clear
enough
for you to understand it. All the detailed info is shown below.
1) mainapp.cpp (creates mainapp)
#include <dlfcn.h>
#include <iostream>
using namespace std;
typedef int (*intfunc) (int, int);
typedef int (*throwfunc) ();
int main()
{
void* pluginHandle = 0;
char* pluginLib = "./libsub.so";
void* gData = 0;
int result = 0;
char* error = 0;
intfunc func1;
throwfunc func2;
try {
pluginHandle = dlopen(pluginLib, RTLD_LAZY);
if (pluginHandle) {
cout << "!! load dll success !!\n";
gData = dlsym(pluginHandle, "gInt");
if ((error = dlerror()) != NULL) {
cout << "dlsym(global1) failed with error<" << error <<
">\n";
return (-1);
}
cout << "\tthe global value is <" << *((int*)(gData)) << ">\n";
func1 = (intfunc)dlsym(pluginHandle, "addInt");
if ((error = dlerror()) != NULL) {
cout << "dlsym(addInt) failed with error<" << error <<
">\n";
return (-1);
}
result = (*func1)(5, 8);
cout << "\tthe function call result is <" << result << ">\n\n";
func2 = (throwfunc)dlsym(pluginHandle, "throwException");
if ((error = dlerror()) != NULL) {
cout << "dlsym(throwException) failed with error<" << error
<< ">\n";
return (-1);
}
result = (*func2)();
dlclose(pluginHandle);
pluginHandle = 0;
} else {
cout << "dlopen failed with error <" << dlerror() << ">\n";
return (-1);
}
} catch (std::string& s) {
cout << "Caught Exception <" << s << ">!" << endl << endl;
if (pluginHandle) {
dlclose(pluginHandle);
}
return (-1);
}
return 0;
}
2) sub.cpp which generates libsub.so
#include <iostream>
int gInt = 5;
extern "C" {
int addInt(int a, int b)
{
std::cout << "Enter addInt ...\n";
return a + b;
}
int throwException()
throw (std::string)
{
throw std::string("Thrown from shared lib");
}
}
3) Makefile
all: libsub.so mainapp
libsub.so:
g++ -g -Wall -c -o sub.o sub.cpp
g++ -shared -o libsub.so sub.o
mainapp: libsub.so
g++ -g -Wall -c -o mainapp.o mainapp.cpp
g++ mainapp.o -L. -lsub -ldl -o mainapp
clean:
rm -f *.o libsub.so mainapp core*
4) correct output:
!! load dll success !!
the global value is <5>
Enter addInt ...
the function call result is <13>
Caught Exception <Thrown from shared lib>!
5) wrong result
!! load dll success !!
the global value is <5>
Enter addInt ...
the function call result is <13>
Abort (core dumped)
6) Core stack:
#0 0x4013ba01 in __kill () at __kill:-1
#1 0x4013b7da in raise (sig=6) at ../sysdeps/posix/raise.c:27
#2 0x4013cf82 in abort () at ../sysdeps/generic/abort.c:88
#3 0x400b7924 in __cxxabiv1::__terminate(void (*)()) (handler=0x4013ce24
<abort>) at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:47
#4 0x400b7968 in std::terminate() ()
at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:57
#5 0x400b7ab5 in __cxa_throw (obj=0x804ab60, tinfo=0x400c4344,
dest=0x400c4344 <__cxxabiv1::__terminate_handler>)
at ../../../../libstdc++-v3/libsupc++/eh_throw.cc:77
#6 0x40018ca5 in throwException () at sub.cpp:16
#7 0x08048d44 in main () at mainapp.cpp:45
#8 0x40129507 in __libc_start_main (main=0x8048b2c <main>, argc=1,
ubp_av=0xbffff4d4, init=0x80488e4 <_init>, fini=0x8048f04 <_fini>,
rtld_fini=0x4000dc14 <_dl_fini>, stack_end=0xbffff4cc)
at ../sysdeps/generic/libc-start.c:129
7) our gcc3.2 configuration info (gcc -v)
Reading specs from /pcc/app/gcc3.2/lib/gcc-lib/i386-redhat-linux/3.2/specs
Configured with:
../configure --prefix=/pcc/app/gcc3.2 --mandir=/pcc/app/gcc3.2/share/man --i
nfodir=/pcc/app/gcc3.2/share/info --enable-shared --enable-threads=posix
--disable-checking --host=i386-redhat-linux --with-system-zlib
--enable-languages=c,c++ --enable-__cxa_atexit
Thread model: posix
gcc version 3.2
8) Redhat 8.0 gcc3.2 info (gcc -v)
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2/specs
Configured with:
../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info
--enable-shared --enable-threads=posix --disable-checking --host=i386-redhat
-linux
--with-system-zlib --enable-__cxa_atexit
Thread model: posix
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)