Subject: libltdl (libtool 2.4.2) and FreeBSD 8.3 i386 (other versions and amd64 versions are also affected)

From: Konstantin Morshnev <[email-masked]>
Date: Wed, 20 Jun 2012 19:22:36 +0400

Dear developers!

We are using libtool in our Parser project (http://www.parser.ru/en/) for about 10 years.
After update to version 2.4.2 (before it was version 1.4.3 :) we've found that libltdl
functionality is broken under FreeBSD in some situations (and it works fine under Linux).

For different SQL drivers Parser uses the following scheme:

Parser3 binary loads libparser3mysql.so dll which in turn loads libmysqlclient.so

Both parser3 and libparser3mysql.so use libltdl. With new libltdl version we've found that parser3
successfully loads libparser3mysql.so, but when libparser3mysql.so calls lt_dlinit it fails.

It was not that easy to debug, as initialization from simple

#if HAVE_LIBDL && !defined(CYGWIN)
errors += lt_dlloader_add (lt_dlloader_next (0), &sys_dl, "dlopen");
#endif

came now to sofisticated call sequence ltdl.c -> preopen.c -> ltdl.c -> preopen.c.

But we've found the problem. On second libltdl load variable 'preloaded_symlists' address
is different in different contexts. We believe it's FreeBSD issue (as same code works fine
under Linux with both included and share libltdl), but libtool is supposed to support existing
OS versions as well, so it would be nice if you'll somehow fix this issue.

Sorry, because it should be binary which calls .so which calls .so, we can't create a test
case for you, but Parser sources can be taken from CVS http://www.parser.ru/en/download/src/
or we can create a sources.tgz for you. Additional information can be provided on your request.

Below is bebugger output:

This is broken case (when libparser3mysql.so calls lt_dlinit()).
You can see preloaded_symlists address change.

lt_dlpreload (preloaded=0x8229be0) at loaders/preopen.c:307
...
275		  preloaded_symlists = tmp;
(gdb) p preloaded_symlists
$7 = (symlist_chain *) 0x0
(gdb) p &preloaded_symlists
$8 = (symlist_chain **) 0x28642524
(gdb) s
322	}
(gdb) p preloaded_symlists
$9 = (symlist_chain *) 0x28505098
(gdb) p &preloaded_symlists
$10 = (symlist_chain **) 0x28642524
(gdb) s
lt_dlinit () at ltdl.c:250
250	      if (!errors)
(gdb) p preloaded_symlists
$11 = (symlist_chain *) 0x0
(gdb) p &preloaded_symlists
$12 = (symlist_chain **) 0x8259128

And thus next call to vm_open fails:

Breakpoint 3, vm_open (loader_data=0x0, filename=0x28506130 "dlopen.a", advise=0x0) at loaders/preopen.c:151
151	  if (!preloaded_symlists)
(gdb) p preloaded_symlists
$13 = (symlist_chain *) 0x0
(gdb) p &preloaded_symlists
$14 = (symlist_chain **) 0x8259128

This is working case (when parser3 calls lt_dlinit() before loading libparser3mysql.so):

lt_dlpreload (preloaded=0x8229be0) at loaders/preopen.c:307
...
275		  preloaded_symlists = tmp;
(gdb) p preloaded_symlists
$13 = (symlist_chain *) 0x0
(gdb) p &preloaded_symlists
$14 = (symlist_chain **) 0x8259128
(gdb) s
322	}
(gdb) p preloaded_symlists
$15 = (symlist_chain *) 0x28505078
(gdb) p &preloaded_symlists
$16 = (symlist_chain **) 0x8259128

And the next call to vm_open works fine:

Breakpoint 6, vm_open (loader_data=0x0, filename=0x28506110 "dlopen.a", advise=0x0) at loaders/preopen.c:151
151	  if (!preloaded_symlists)
(gdb) p preloaded_symlists
$17 = (symlist_chain *) 0x28505078
(gdb) p &preloaded_symlists
$18 = (symlist_chain **) 0x8259128

P.S. It would be nice it you'll also simplify Microsoft Visual Studio integration and instead of
copying argz_.h into argz.h do something like

#ifdef HAVE_WORKING_ARGZ
#include <argz.h>
#else
#include <argz_.h>
#endif

WBR, MoKo