Bug #18267 pthreads library in single-threaded libraries causes dlopen() to fail on HP/UX
Submitted: 16 Mar 2006 0:47 Modified: 5 Oct 2006 14:57
Reporter: [ name withheld ] Email Updates:
Status: Closed Impact on me:
Category:MySQL Server: Compiling Severity:S2 (Serious)
Version:5.0.24-BK, 5.0.19 OS:Linux (Linux, HP/UX)
Assigned to: Ramil Kalimullin

[16 Mar 2006 0:47] [ name withheld ]
When building MySQL 5.0.x, linking pthreads into single-threaded MySQL 5.0
libraries causes the following error on HP/UX systems when the libraries are
loaded at runtime: "Unable to open libmysqlclient.sl: Can't dlopen() a library
containing Thread Local Storage: /usr/lib/libpthread.1".

How to repeat:
#include <iostream>
#include <dlfcn.h>

extern int
main (int argc, char **argv) {
  if (argc != 2) {
    std::cerr << "Usage: " << argv[0]
              << " library" << std::endl;
    exit (0);

  void* lib = dlopen (argv[1], RTLD_NOW);
  if (!lib) {
    std::cerr << "dlopen() failed: " << dlerror()
              << std::endl;
    exit (-1);

  std::cout << "Library " << lib << " loaded."
             << std::endl;
  dlclose (lib);
  return 0;

> aCC -AA -g +w +Z -O test01.cpp  -o test01
> ./test01 $MYSQL/lib/mysql/libz.sl.3.3
dlopen() failed: Can't dlopen() a library containing Thread Local Storage: /usr/lib/libpthread.1

Suggested fix:
The pthreads library should be linked only when absolutely necessary at least on
HP/UX platforms.
[23 Mar 2006 16:08] Valerii Kravchuk
Thank you for a problem report. Please, check the newer version of MySQL server, 5.0.19, and reopen this report in case it has similar problem. The uname -a results from your system might be also useful.
[23 Mar 2006 21:55] [ name withheld ]
Yes the bug exists in all versions since released.  I downloaded and did a build of the most recent version on the problematic platform and the pthreads library is still linked into single-threaded libraries.

Uname -a produces the following:
HP-UX moe B.11.11 U 9000/800 1687329373 unlimited-user license
[6 Apr 2006 14:44] Valerii Kravchuk
Sorry for a long delay with this bug report. Do you build 5.0.19 yourself from sources (then give configure options used) or use MySQL's binaries (then give exact URL used)?
[6 Apr 2006 14:48] Valerii Kravchuk
Please, send the 

mysql_config --libs

results, just to check. Can be similar to bug #18322.
[6 Apr 2006 17:45] [ name withheld ]
The binary downloads only contain the static libraries and, as we also need shared libraries, we have to build MySQL from source.  IIRC, the configure options are specified as: "--enable-thread-safe-client".  "mysql_config --libs" gives this: "-L/nfs/packages/mdx/hpux/databases/mysql/mysql-source-pic-5.0.18-hp-hpux11.11-hppa2.0w/lib/mysql -lmysqlclient -lz -lcrypt -lnsl -lm".
[18 Apr 2006 18:04] [ name withheld ]
After further testing, it appears that the linker on HP/UX systems is rather stupid: it links in any library you tell it to whether it is needed or not.  This is the case with the prepackaged zlib source in the MySQL source distribution.  This particular problem can be solved by removing the '-lpthreads' link flags from the mysql-5.0.x/zlib/Makefile since zlib does not need the pthreads library.  I just tested this change with the 5.0.20 source distribution.
[21 Apr 2006 14:01] Valerii Kravchuk
Sorry, but if you created mysqlclientlibrary as multithreaded (--enable-thread-safe-client), then libz should be also theread-safe, isn't it? If you just compile as you proposed you may get incorrect context in your multithreaded applications.

Please, try to configure without that --enable-thread-safe-client and check, if libz will have a libpthread dependency.
[21 Apr 2006 16:22] [ name withheld ]
Without the '--enable-thread-safe-client', MySQL is configured and built with only the single-threaded libraries.  With the option, both single-threaded and multi-threaded libraries are built.  It's the multi-threaded MySQL code that requires pthreads, not zlib.  zlib is inherently single-threaded.  It does not need nor require the pthreads library.  And on this particular platform, it actually causes an error when loaded at runtime in single-threaded programs.

So if zlib is linked to pthreads and then linked to the multi-threaded MySQL client libraries, that's fine because the multi-threading in MySQL requires pthreads, not zlib.  However, if the single-threaded MySQL clients link in zlib which links in pthreads, you'll get an error when loading any of these libraries at runtime.

Bottom line: link pthreads to the multi-threaded MySQL libraries and nowhere else.
[12 May 2006 18:32] Valerii Kravchuk
Please, try to repeat with a newer version, 5.0.21, and inform about the results.
[12 May 2006 18:59] [ name withheld ]
Same result.
[12 May 2006 20:18] [ name withheld ]
There is an extremely easy fix for this problem.

Have your release managers/maintainers apply the following patch and regenerate the distribution files before your next maintenance release.  Should work on all MySQL supported platforms.

*** zlib/Makefile.am.orig       Wed Apr 26 11:31:10 2006
--- zlib/Makefile.am    Fri May 12 13:14:33 2006
*** 17,22 ****
--- 17,23 ----
  # Process this file with automake to create Makefile.in

  INCLUDES=             -I$(top_builddir)/include -I$(top_srcdir)/include
+ LIBS=                 $(NON_THREADED_LIBS)

[3 Jul 2006 15:05] Valerii Kravchuk
Indeed, libz does not need to depend on pthreads by itself. While it is dependent, even on Linux:

openxs@suse:~/dbs/5.0/mysql-test> ldd ~/dbs/5.0/lib/mysql/libz.so
        linux-gate.so.1 =>  (0xffffe000)
        libpthread.so.0 => /lib/tls/libpthread.so.0 (0x40028000)
        libcrypt.so.1 => /lib/libcrypt.so.1 (0x4003a000)
        libnsl.so.1 => /lib/libnsl.so.1 (0x4006c000)
        libm.so.6 => /lib/tls/libm.so.6 (0x40081000)
        libc.so.6 => /lib/tls/libc.so.6 (0x400a4000)
        /lib/ld-linux.so.2 (0x80000000)

Verified with 5.0.24-BK.
[16 Aug 2006 9:33] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:


ChangeSet@1.2255, 2006-08-16 14:34:08+05:00, ramil@mysql.com +1 -0
  fix for bug #18267: pthreads library in single-threaded libraries causes dlopen() to fail on HP/UX
    - zlib doesn't need a thread lib.
[16 Aug 2006 9:34] Ramil Kalimullin
Eric, thanks a lot for your help!
[3 Oct 2006 20:03] Chad MILLER
Available in 5.0.26.
[3 Oct 2006 20:10] Chad MILLER
Available in 5.1.12-beta.
[5 Oct 2006 14:57] Paul Dubois
Noted in 5.0.26, 5.1.12 changelogs.

Linking the pthreads library to single-threaded MySQL libraries
caused dlopen() to fail at runtime on HP-UX.