<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr">On Fri, Dec 21, 2018 at 4:21 PM Lev Olshvang <<a href="mailto:levonshe@yandex.com">levonshe@yandex.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
Hi All,<br>
<br>
I have an executable (C++) which is the exclusive user of the some shared library that it uses only during the initialization phase.<br>
<br>
I would like to free memory used by this shared library, because I am running on embedded system.<br>
<br>
How can I achieve this?<br>
<br>
I know that dlopen() will load shared library, and hope that following dlclose() will free this lib memory. <br></blockquote><div><br></div><div>According to the dlclose (2) man page:</div><div> "The function dlclose() decrements the reference count on the dynamically loaded shared object referred to by handle. If the reference count drops to zero, then the object is unloaded. All shared<br> objects that were automatically loaded when dlopen() was invoked on the object referred to by handle are recursively closed in the same manner.<br><br> A successful return from dlclose() does not guarantee that the symbols associated with handle are removed from the caller's address space. In addition to references resulting from explicit dlopen()<br> calls, a shared object may have been implicitly loaded (and reference counted) because of dependencies in other shared objects. Only when all references have been released can the shared object be<br> removed from the address space."</div><div><br></div><div></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
1. Still I do not know what method should be used to make dynamic linker look again into executable and resolve symbols of the newly appeared symbols ?<br></blockquote><div><br></div><div>If you are using the dlopen/dlclose functions, you are responsible for symbol resolution for symbols provided by your shared object. After you get a handle from dlopen(), you need to call dlsym() in order to get a pointer to your symbol. You can then call that symbol when you need it.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
2. And how to tell the dynamic linker ld-linux.so to postpone the symbol resolution until dlopen() will load the library?<br></blockquote><div><br></div><div>When you use dlopen() your compilation shouldn't need to resolve any symbols from your dlopen()-ed libraries.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
3. Whether to compile and link executable with this library or leave unresolved symbols?<br></blockquote><div><br></div><div>You shouldn't have any unresolved symbols, nor should you link to your library during compilation. I guess the best thing is to show an example:</div><div><br></div><div>Given the following library source (printint.c) :</div><div><br></div><div>------------------------</div><div>#include <stdio.h></div><div><br></div><div>void printint(int num) {</div><div> printf("Called with num=%d \n", num);<br></div><div>}</div><div>------------------------</div><div><br></div><div>Create a shared object from it using <br></div><div> gcc -shared -o libprintint.so printint.c<br></div><div> </div><div>Now consider the following test program (main.c) which uses printint with dlopen (removed most of the error handling for clarity here):<br></div><div>-------------------</div><div>#include <dlfcn.h><br></div><div></div><div><br>typedef void printint(int num);</div><div><br></div><div>int main(int argc, char *argv[]) {<br> void *handle = NULL;<br> handle = dlopen("./libprintint.so", RTLD_LAZY);<br> if (handle == NULL ) {<br><div> // use dlerror to find out what went wrong<br></div> return -1;<br> }<br> printint *sym = NULL;<br> sym = (printint*)dlsym(handle, "printint");<br> if (sym == NULL ) {</div><div> // use dlerror to find out what went wrong<br></div><div> return -1;<br> }<br> sym(argc);<br> dlclose(handle);<br> return 0;<br>}<br></div><div>--------------</div><div><br></div><div>You compile this program like this: <br></div><div><br></div><div> gcc main.c -ldl -o a.out<br></div><div><br></div><div>You can verify that the program doesn't dynamically link to libprintint.so by running "ldd ./a.out". When you run it with libprintit.so in the same directory it will load the shared library and call the correct function.<br></div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
Waiting for your advises,<br>
Lev.<br>
<br>
_______________________________________________<br>
Linux-il mailing list<br>
<a href="mailto:Linux-il@cs.huji.ac.il" target="_blank">Linux-il@cs.huji.ac.il</a><br>
<a href="http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il" rel="noreferrer" target="_blank">http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il</a><br>
</blockquote></div></div></div></div></div></div>