This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Inconsistencies with RTLD_DEEPBIND and dependency libraries in global scope


When using RTLD_DEEPBIND to load a dynamic library that depends on
another library that is already opened, the global instance of the
library is reused and so its symbol references are resolved from
the global scope.  (I can imagine that loading another instance of
the dependency so that relocations could be performed in a
RTLD_DEEPBIND manner may be unappealing.)

However, the dynamic library opened with RTLD_DEEPBIND will lookup
symbols defined in its dependency (which is in the global scope)
using the local scope first.  This can mean that the dynamic
library ends up using a definition in the dependency even when the
dependency is using a different definition.

e.g.

% cat libdep.c
int duplicate = 'u';

int get_duplicate() {
  return duplicate;
}
% gcc -shared -fPIC libdep.c -o libdep.so
% cat dynamic.c
#include <stdio.h>

extern int duplicate;

int run() {
  duplicate = 'd';
  printf("dynamic sees duplicate from libdep as:  %c\n", duplicate);
  printf("but libdep sees duplicate from main as: %c\n", get_duplicate());
  return 0;
}
% gcc -shared -fPIC dynamic.c -Wl,-rpath,. -L. -ldep -o dynamic.so
% cat main.c
#include <dlfcn.h>
#include <stdlib.h>

extern int duplicate;

int main() {
  void *h;
  int (*run)();

  duplicate = 'm';

  h = dlopen("./dynamic.so", RTLD_LAZY | RTLD_DEEPBIND);
  if (!h)
    abort();

  run = dlsym(h, "run");
  if (!run)
    abort();

  (*run)();
}
% gcc main.c -Wl,-rpath,. -L. -ldep -ldl
% ./a.out
dynamic sees duplicate from libdep as:  d
but libdep sees duplicate from main as: m

I understand that it is best to avoid RTLD_DEEPBIND where possible, but
apparently it has been necessary sometimes.

If a dynamic library is looking for a symbol in a dependency that has a
definition of the symbol, then would it make sense for the dynamic library to
use the same definition that is used by that dependency?

Or is it important that the dynamic library uses the local definition even
when the definition is provided by a library in global scope?

Thanks,
Karl.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]