[Bug 945927] Re: ld incorrectly gives 'undefined reference'

CF freysoldt at mpie.de
Tue Mar 13 10:13:18 UTC 2012


Hi Matthias,

> libbar.so needs to be linked with libm.

I disagree. libbar,so should have been linked with -lm to circumvent the problem, but it happens not to be like this. I looked around in the internet, but found nowhere written that a valid shared object *must* have all dependencies resolved when it is created. Rather, there are good reasons to leave this open:
- use of standardized interfaces, that may be provided by a variety of libraries (e.g. MPI, BLAS)
- linkage to an optimized library for single-threaded/multi-threaded programs (e.g. mkl)
According to my understanding, a bug-free linker must be able to deal with the situation that some symbols in the shared object will only be resolved with additional libraries that the shared object does not know about. The current linker does this correctly if this particular library is not indirectly used by a second (libfoo) library *offered* to the linker. If the linker figures out that libfoo is not needed, it MUST not conclude that the explicit -lm appearing later on is not needed.

In summary:
- libfoo.so and libbar.so are valid shared objects
- the dependency of libbar on libm is resolved in the linker command line
=> the linker MUST be able to link everything together

Please accept that the current behavior of ld is a misbehavior.

I am saying this not to annoy you, but because the current setup causes
us severe headache to build up a versatile set of libraries for high-
performance computing that ultimately rely on highly specialized low-
level numerical libraries with standardized interfaces.

Many thanks,

Christoph

-- 
You received this bug notification because you are a member of Ubuntu
Foundations Bugs, which is subscribed to binutils in Ubuntu.
https://bugs.launchpad.net/bugs/945927

Title:
  ld incorrectly gives 'undefined reference'

Status in “binutils” package in Ubuntu:
  Invalid

Bug description:
  I had an unexpected link problem while trying to probe libraries in a
  configure script.

  I am using the latest update (2012-03-03 21:00 MEZ) of kubuntu 11.10, the binutils package is 2.21.53.20110810-0ubuntu5.1
  My machine is a  Intel(R) Core(TM)2 Duo CPU     E8400  @ 3.00GHz, but I doubt that matters.

   I was able to cook the problem down to the following.

  Situation:
  libfoo.so needs libm, and was created with -lm (ldd shows the dependency)
  libbar.so needs libm, but was not created with -lm (no ldd dependency)
  My program only needs libbar.so

  Problem:

  gcc prog.c -L. -lbar -lfoo -lm

  yields an 'undefined reference to sinh'
  If I run the link with -Wl,--trace-symbol=sinh, the linker actually reports that -lm contains sinh, but fails nevertheless.

  What works is
  gcc prog.c -L. -lbar -lm
  gcc prog.c -L. -lbar -lm -lfoo

  [The example of course cries for a simple workaround by reordering the
  libs, but this is a mess when you try it in the configure. So just
  take the lib ordering for granted (and this configure works on dozens
  of other systems). I also know that libbar isn't up to the standards -
  but, in real life, I won't change the way how other packages create
  their shared libs.]

  Here's some simple C files to reproduce the bug
  --- a.c:
  #include <math.h>
  double foo (double x) { return exp(x); }
  ---- b.c:
  #include <math.h>
  double bar(double x) { return sinh(x); }
  ---- c.c:
  double bar (double);
  int main ()
  {
     double x = bar (1.);
  }
  ---
  gcc -fPIC -c a.c
  gcc -shared -fPIC a.o -o libfoo.so
  gcc -fPIC -c b.c
  gcc -shared -fPIC b.o -o libbar.so

  gcc c.c -L. -lbar -lfoo -lm

  ProblemType: Bug
  DistroRelease: Ubuntu 11.10
  Package: binutils 2.21.53.20110810-0ubuntu5.1
  ProcVersionSignature: Ubuntu 3.0.0-16.28-generic 3.0.17
  Uname: Linux 3.0.0-16-generic x86_64
  ApportVersion: 1.23-0ubuntu4
  Architecture: amd64
  Date: Sat Mar  3 23:08:39 2012
  InstallationMedia: Kubuntu 10.04.1 LTS "Lucid Lynx" - Release amd64 (20100816.2)
  ProcEnviron:
   LANGUAGE=
   PATH=(custom, user)
   LANG=de_DE.UTF-8
   SHELL=/bin/tcsh
  SourcePackage: binutils
  UpgradeStatus: Upgraded to oneiric on 2012-02-29 (3 days ago)

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/binutils/+bug/945927/+subscriptions




More information about the foundations-bugs mailing list