<div xmlns="http://www.w3.org/1999/xhtml">Hi Lior</div><div xmlns="http://www.w3.org/1999/xhtml"> </div><div xmlns="http://www.w3.org/1999/xhtml">thanks for your time and code example.</div><div xmlns="http://www.w3.org/1999/xhtml"> </div><div xmlns="http://www.w3.org/1999/xhtml">Shachar Shemesh explained to me couple of simple things I forgotten.</div><div xmlns="http://www.w3.org/1999/xhtml"> </div><div xmlns="http://www.w3.org/1999/xhtml">You can find his replies in this thread but the bottom line :</div><div xmlns="http://www.w3.org/1999/xhtml"> </div><div xmlns="http://www.w3.org/1999/xhtml">I do not need to signal kernel.</div><div xmlns="http://www.w3.org/1999/xhtml">Kernel memory manager will discover that pages are not active ( each page have active bit) so it will use it whenever  get free page request is received.</div><div xmlns="http://www.w3.org/1999/xhtml"> </div><div xmlns="http://www.w3.org/1999/xhtml">And the second option is to look  mapped memory for this loib /proc/pid/maps</div><div xmlns="http://www.w3.org/1999/xhtml">and issue munmap () call.</div><div xmlns="http://www.w3.org/1999/xhtml"> </div><div xmlns="http://www.w3.org/1999/xhtml">simple and easy.</div><div xmlns="http://www.w3.org/1999/xhtml"> </div><div xmlns="http://www.w3.org/1999/xhtml">Thanks to given it a thought.</div><div xmlns="http://www.w3.org/1999/xhtml">Lev</div><div xmlns="http://www.w3.org/1999/xhtml"> </div><div xmlns="http://www.w3.org/1999/xhtml"> </div><div xmlns="http://www.w3.org/1999/xhtml"> </div><div xmlns="http://www.w3.org/1999/xhtml">08.01.2019, 05:52, "Lior Okman" <lior@okman.name>:</div><blockquote xmlns="http://www.w3.org/1999/xhtml" type="cite"><div><div> </div> <div>On Fri, Dec 21, 2018 at 4:21 PM Lev Olshvang <<a rel="noopener noreferrer" href="mailto:levonshe@yandex.com">levonshe@yandex.com</a>> wrote:</div><blockquote 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.</blockquote><div> </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> </div><div> </div><div> </div><blockquote 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 ?</blockquote><div> </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.</div><div> </div><blockquote 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?</blockquote><div> </div><div>When you use dlopen() your compilation shouldn't need to resolve any symbols from your dlopen()-ed libraries.</div><div> </div><blockquote 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?</blockquote><div> </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> </div><div>Given the following library source (printint.c) :</div><div> </div><div>------------------------</div><div>#include <stdio.h></div><div> </div><div>void printint(int num) {</div><div>    printf("Called with num=%d \n", num);</div><div>}</div><div>------------------------</div><div> </div><div>Create a shared object from it using </div><div>         gcc -shared -o libprintint.so  printint.c</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):</div><div>-------------------</div><div>#include <dlfcn.h></div><div> </div><div><br />typedef void printint(int num);</div><div> </div><div>int main(int argc, char *argv[]) {<br />    void *handle = NULL;<br />    handle = dlopen("./libprintint.so", RTLD_LAZY);<br />    if (handle == NULL ) {<div>        // use dlerror to find out what went wrong</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</div><div>        return -1;<br />    }<br />    sym(argc);<br />    dlclose(handle);<br />    return 0;<br />}</div><div>--------------</div><div> </div><div>You compile this program like this:</div><div> </div><div>              gcc  main.c -ldl -o a.out</div><div> </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.</div><div> </div><div> </div><blockquote 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 target="_blank" rel="noopener noreferrer" href="mailto:Linux-il@cs.huji.ac.il">Linux-il@cs.huji.ac.il</a><br /><a target="_blank" rel="noopener noreferrer" href="http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il">http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il</a></blockquote></div>,<p>_______________________________________________<br />Kernelnewbies mailing list<br /><a rel="noopener noreferrer" href="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a><br /><a rel="noopener noreferrer" href="https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies">https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a></p></blockquote>