Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symbol lookup error at runtime instead of load time

Tags:

c++

linux

ld

I have an application which uses a class Foo from an .so shared library. I've come across a problem where at runtime it prints

<appname>: symbol lookup error: <appname>: undefined symbol: <mangled_Foo_symbol_name>

Now, it turned out that the unmangled symbol was for the constructor of the class Foo, and the problem was simply that an old version of the library was loaded, which didn't contain Foo yet.

My question isn't about resolving the error (that's obviously to use the correct library), but why it appears at runtime instead of at time of load / startup.

The line of code causing the error just instantiates an object of class Foo, so I'm not using anything like dlopen here, at least not explicitly / to my knowledge.

In contrast, if I remove the whole library from the load search path, I get this error at startup:

<appname>: error while loading shared libraries: libname.so.2: cannot open shared object file: No such file or directory

When the wrong version of gcc / libstdc++ is on the load path, an error also appears at starup:

<appname>: /path/to/gcc-4.8.0/lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by <appname>)


This "fail fast" behavior is much more desirable, I don't want to run my application for quite awhile first, until I finally realize it's using the wrong library. What causes the load error to appear at runtime and how can I make it appear immediately?

like image 929
Andre Avatar asked Nov 09 '22 04:11

Andre


1 Answers

From the man page of ld.so:

ENVIRONMENT

  • LD_BIND_NOW (libc5; glibc since 2.1.1) If set to a nonempty string, causes the dynamic linker to resolve all symbols at program startup instead of deferring function call resolution to the point when they are first referenced. This is useful when using a debugger.
  • LD_WARN (ELF only)(glibc since 2.1.3) If set to a nonempty string, warn about unresolved symbols.
like image 62
Toby Speight Avatar answered Nov 14 '22 21:11

Toby Speight