Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unloading a shared library from memory

I am trying to modify this shared library (with .so) extension on Linux. I am inserting some printf statement and fprintf statement to debug, and it has no effect. I removed the .so file and realized that the the program still runs fine. Does it mean that the program is loaded into memory?? (But I'm sure only the program I'm testing for uses that .so file though)

How do I get it to unload so I can make sure my program is loading the modified one?

like image 375
huggie Avatar asked Jul 23 '12 03:07

huggie


People also ask

How are shared libraries loaded?

Shared libraries are the most common way to manage dependencies on Linux systems. These shared resources are loaded into memory before the application starts, and when several processes require the same library, it will be loaded only once on the system. This feature saves on memory usage by the application.

How do shared libraries work?

Simply put, A shared library/ Dynamic Library is a library that is loaded dynamically at runtime for each application that requires it. Dynamic Linking doesn't require the code to be copied, it is done by just placing name of the library in the binary file.

How are shared libraries created?

To create a shared library in C++ using G++, compile the C++ library code using GCC/ G++ to object file and convert the object file to shared (. SO) file using gcc/ g++. The code can be used during the compilation of code and running executable by linking the . SO file using G++.

How do I install a shared library?

Once you've created a shared library, you'll want to install it. The simple approach is simply to copy the library into one of the standard directories (e.g., /usr/lib) and run ldconfig(8). Finally, when you compile your programs, you'll need to tell the linker about any static and shared libraries that you're using.


2 Answers

No, shared libraries are not cached in memory. If you have deleted the .so file and your program still runs, then either:

  • the program is loading an .so of the same name from a different location, or
  • the program can run without loading the .so

If the .so is supposed to be loaded at program startup, then you can use ldd to find out where your OS thinks the .so actually is.

If the .so is loaded dynamically at runtime, then perhaps strace will be able to help pinpoint what is happening.

like image 163
Greg Hewgill Avatar answered Oct 23 '22 00:10

Greg Hewgill


You can read /proc/1234/maps to find out the memory map of process 1234. This also shows the dynamically loaded shared objects.

You may use the LD_LIBRARY_PATH environment variable to change the path of shared libraries and ldconfig to upgrade its cache. Look also in /etc/ld.so.conf etc.

Of course, you have to restart the program loading your shared library.

like image 21
Basile Starynkevitch Avatar answered Oct 23 '22 00:10

Basile Starynkevitch