0

I have some non-accessible code that I call, that does dlopen("lib.so", RTLD_LOCAL).

The problem is that I need to control the search path of dlopen(). The answer to this problem is quite typically "set LD_LIBRARY_PATH", but I don't know the actual path to set until after application startup, so I can't put a wrapper script that sets it and then invokes my application.

According to the documentation of ld.so and of dlopen, LD_LIBRARY_PATH is only examined at application startup. If you change it afterwards inside the application with setenv, it won't change the lookup list of dlopen().

I know that specifying the full path to dlopen() would be a strategy, but I don't have access to that dlopen call, so this option is also not possible.

Am I out of options or is there some magic strategy I can't find?

Stefano Borini
  • 138,652
  • 96
  • 297
  • 431

2 Answers2

1

I believe it is not easily possible.

However, if you are crazy enough to patch ld.so from its source code, you might do something.

Maybe you could use some LD_PRELOAD trick.

But if it is a matter of finding which exact file is dlopen-ed, why don't you strace(1) your program to understand which files are mmap-ed?

You can also use pmap or simply cat /proc/$(pidof your-program)/maps

If you can change some lines of source code, consider dladdr(3) to find out where is some dlsym-ed function... And you might also use dl_iterate_phdr(3)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • It's a long story. I tried to make it easy, but the true need is because I need to override libncurses with libncursesw (wide char support) in a python application. The _curses module binds against libncurses, so what I do is that I copy libncursesw as libncurses in a user-accessible dir, then override the search path and import _curses so that it resolves against the curses with wide char support. It was just a curiosity to make my code more user friendly, but I can find out alternative strategies... – Stefano Borini Feb 04 '15 at 12:30
  • You should have told that in your question. I would believe you should handle that in python. – Basile Starynkevitch Feb 04 '15 at 12:31
  • I can't handle that in python. _curses.so is linked to libncurses.so. which is a system lib, so I can't touch it. My question is generic because my scope is well defined: override dlopen search strategy after startup. If I solve that, I solve my specific issue. – Stefano Borini Feb 04 '15 at 12:33
1

If your LD_LIBRARY_PATH is relative to your application root - you can use wrapper script, which will extract path to itself using $(dirname $0) and set up correct LD_LIBRARY_PATH.

Another trick (but it's not a good idea to do so) is to provide your own lib.so that will be just a proxy to actual lib.so. You can initialize all references on your proxy library load using library init functionality. Please refer to this question.

Community
  • 1
  • 1
iwlagn
  • 477
  • 3
  • 10