Search code examples
clinuxelfdlopen

Is there a way to load a Linux shared library into a specific memory location?


I have a Linux application which loads in very small (a few small functions) shared libraries at run-time. For various Important Reasons™ I need the shared libraries to be loaded into a certain virtual memory range. However, dlopen() doesn't provide any means (that I can see) to tell it, or hint to it, where to put what it loads.

Is there a way to tell dlopen() where it should put the library it loads?

Is there some alternative to dlopen() which would provide that functionality?


Solution

  • I think that the prelink program may actually demonstrate a way to do this if you are willing to modify the library. The goal is to modify the library so that it will have a preferred address. The intent of prelink is to do this for performance, but my suspicion is that it can be modified to work for your use case. Note that you will never be guaranteed that this will happen in the general case, but in controlled cases you may be able to guarantee that it will happen. Certainly examining prelink will allow you to understand the parts of elf involved and make a more informed determination about whether it is possible. http://en.wikipedia.org/wiki/Prelink# Or see http://packages.qa.debian.org/prelink for the sources in Debian.