Loading a shared library in HP-UX (362 Views)
Reply
Occasional Visitor
Michael Forest
Posts: 2
Registered: ‎03-18-2011
Message 1 of 5 (362 Views)

Loading a shared library in HP-UX

I've been trying to get this piece of C++ code to work for 2 days now, and I'm hoping someone might be able to point me in the right direction. Whenever I try to load a particular shared library I get this error:

Cannot dlopen load module '/opt/java1.5/jre/lib/PA_RISC2.0W/server/libjvm.sl' because it contains thread specific data.

I have no idea what this means, and Google has not been very helpful explaining what this is actually saying. Here is some information on the environment:

# uname -a
HP-UX neo B.11.11 U 9000/800 101901537 unlimited-user license

# /opt/java1.5/bin/java -version
java version "1.5.0.19"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0.19-_18_dec_2009_00_08)
Java HotSpot(TM) Server VM (build 1.5.0.19 jinteg:12.18.09-02:30 PA2.0 (aCC_AP), mixed mode)

# aCC --version
aCC: HP ANSI C++ B3910B A.03.85

This is how I compile my code:

# aCC -mt +Z +DA2.0W -c test.cpp
# ld -o test -lc test.o
# ./test
Cannot dlopen load module '/opt/java1.5/jre/lib/PA_RISC2.0W/server/libjvm.sl' because it contains thread specific data.

This is my C++ code:

#include
#include
#include
#include

int main(void){
void* handle;
handle = dlopen("/opt/java1.5/jre/lib/PA_RISC2.0W/server/libjvm.sl", RTLD_NOW);
if(handle == NULL){
printf("%s\n", dlerror());
}
else {
printf("ok\n");
dlclose(handle);
}
}

Any help with this would be very much appreciated!
Honored Contributor
Matti_Kurkela
Posts: 6,271
Registered: ‎12-02-2001
Message 2 of 5 (362 Views)

Re: Loading a shared library in HP-UX

I'm not really a programmer, so I might be off-base here.

But here's what I've understood about your situation:

"thread-specific data" might be more commonly known as Thread-Local Storage (TLS). It means the library must reserve some amount of memory for the library's internal workspace for each thread this process is going to run.

When the program is initially loaded, this is not a problem: the dynamic loader loads the program and all the libraries it requires, finds the TLS requirements (if any) of each library and puts the value to some place where the thread creation functions can find it. Then it runs the initialization code(if any) for each library, and ultimately the program itself.

But when you dlopen() a library in mid-program, and that library requires Thread-Local Storage, there is a problem: the dlopen() function would have to stop all the threads this program may be using, and increase the TLS assigned for each one. But this would require moving things around in memory, and *that* would invalidate all the pointers and similar memory references the program may have created. Apparently there is no good solution to this problem, because Google indicates the situation is much the same in other Unixes, Linux and Windows too.

Therefore, you aren't permitted to use dlopen() to load a library that contains TLS: you must link your program with it, so the dynamic loader knows to load the library *before* the main() function of your program begins executing.

The library you're loading is libjvm.sl, the core of the Java Virtual Machine. Java makes heavy use of threads: I wouldn't be too surprised if the initialization code of libjvm.sl set up some JVM-internal threads on its own.

MK
MK
Occasional Visitor
Michael Forest
Posts: 2
Registered: ‎03-18-2011
Message 3 of 5 (362 Views)

Re: Loading a shared library in HP-UX

Here's the catch though: this exact piece of code compiles just fine on both Windows and Linux. I just tested it on my laptop and other than adjusting the path to libjvm, it works.

$ g++ -ldl -o test test.cpp
$ ./test
ok
$
Acclaimed Contributor
Dennis Handly
Posts: 25,188
Registered: ‎03-06-2006
Message 4 of 5 (362 Views)

Re: Loading a shared library in HP-UX

>Cannot dlopen load module 'libjvm.sl' because it contains thread specific data.

It means what it says. You can't dynamically load that shlib at all on PA.
You'll have to statically link in that shlib.
Or use:
LD_PRELOAD=/opt/java1.5/jre/lib/PA_RISC2.0W/server/libjvm.sl executable

>this exact piece of code runs just fine on both Windows and Linux.

And also on HP-UX Integrity.

>MK: Apparently there is no good solution to this problem

The problem is solved on Integrity since +tls=dynamic is the default.
Acclaimed Contributor
Dennis Handly
Posts: 25,188
Registered: ‎03-06-2006
Message 5 of 5 (362 Views)

Re: Loading a shared library in HP-UX

>Google has not been very helpful explaining what this is actually saying.

Did you try:
"Cannot dlopen load module" site:itrc.hp.com
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.