Kernel Newbies archive on lore.kernel.org
 help / Atom feed
* How to signal kernel that shared library is not in use by any process anymore ?
@ 2018-12-21 14:20 Lev Olshvang
  2018-12-21 15:31 ` Greg KH
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Lev Olshvang @ 2018-12-21 14:20 UTC (permalink / raw)
  To: kernelnewbies, linux-il


Hi All,

I have  an executable (C++) which is the exclusive user of the some shared library that it uses only during  the initialization phase.

I would like to free memory used by this shared library, because I am running on embedded system.

How can I achieve this?

I know that dlopen() will load shared library, and hope that following dlclose() will free this lib memory. 

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 ?
2. And  how to tell the dynamic linker ld-linux.so to postpone the symbol resolution until dlopen()  will load  the library?
3. Whether to compile and link executable with this library or leave unresolved symbols?

Waiting for your advises,
Lev.

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to signal kernel that shared library is not in use by any process anymore ?
  2018-12-21 14:20 How to signal kernel that shared library is not in use by any process anymore ? Lev Olshvang
@ 2018-12-21 15:31 ` Greg KH
  2018-12-27  7:47   ` Lev Olshvang
  2018-12-21 16:37 ` Lior Okman
  2018-12-22  2:34 ` Bernd Petrovitsch
  2 siblings, 1 reply; 7+ messages in thread
From: Greg KH @ 2018-12-21 15:31 UTC (permalink / raw)
  To: Lev Olshvang; +Cc: linux-il, kernelnewbies

On Fri, Dec 21, 2018 at 05:20:36PM +0300, Lev Olshvang wrote:
> 
> Hi All,
> 
> I have  an executable (C++) which is the exclusive user of the some shared library that it uses only during  the initialization phase.
> 
> I would like to free memory used by this shared library, because I am running on embedded system.
> 
> How can I achieve this?
> 
> I know that dlopen() will load shared library, and hope that following dlclose() will free this lib memory. 

That right there is how you "achieve" this, call dlclose() and all will
be fine.  If your system needs the memory that was being used, it will
reclaim it from the memory that was previously being used by the library
at this point in time.

Nothing else needs to be done.

Have you tested this and found it not to work properly?

> 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 ?

What "newly appeared symbols"?

If you need to load the library again, call dlopen().

> 2. And  how to tell the dynamic linker ld-linux.so to postpone the symbol resolution until dlopen()  will load  the library?

It will not happen until you tell it to, right?

> 3. Whether to compile and link executable with this library or leave unresolved symbols?

It sounds like you don't really know what type of problem you are trying
to solve here.

Back up, what is the real issue you are having with the kernel at this
point in time?

thanks,

greg k-h

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to signal kernel that shared library is not in use by any process anymore ?
  2018-12-21 14:20 How to signal kernel that shared library is not in use by any process anymore ? Lev Olshvang
  2018-12-21 15:31 ` Greg KH
@ 2018-12-21 16:37 ` Lior Okman
  2019-01-08 15:56   ` Lev Olshvang
  2018-12-22  2:34 ` Bernd Petrovitsch
  2 siblings, 1 reply; 7+ messages in thread
From: Lior Okman @ 2018-12-21 16:37 UTC (permalink / raw)
  To: Lev Olshvang; +Cc: linux-il, kernelnewbies

[-- Attachment #1.1: Type: text/plain, Size: 3708 bytes --]

On Fri, Dec 21, 2018 at 4:21 PM Lev Olshvang <levonshe@yandex.com> wrote:

>
> Hi All,
>
> I have  an executable (C++) which is the exclusive user of the some shared
> library that it uses only during  the initialization phase.
>
> I would like to free memory used by this shared library, because I am
> running on embedded system.
>
> How can I achieve this?
>
> I know that dlopen() will load shared library, and hope that following
> dlclose() will free this lib memory.
>

According to the dlclose (2) man page:
      "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
       objects that were automatically loaded when dlopen() was invoked on
the object referred to by handle are recursively closed in the same manner.

       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()
       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
       removed from the address space."



> 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 ?
>

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.


> 2. And  how to tell the dynamic linker ld-linux.so to postpone the symbol
> resolution until dlopen()  will load  the library?
>

When you use dlopen() your compilation shouldn't need to resolve any
symbols from your dlopen()-ed libraries.


> 3. Whether to compile and link executable with this library or leave
> unresolved symbols?
>

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:

Given the following library source (printint.c) :

------------------------
#include <stdio.h>

void printint(int num) {
    printf("Called with num=%d \n", num);
}
------------------------

Create a shared object from it using
         gcc -shared -o libprintint.so  printint.c

Now consider the following test program (main.c) which uses printint with
dlopen (removed most of the error handling for clarity here):
-------------------
#include <dlfcn.h>

typedef void printint(int num);

int main(int argc, char *argv[]) {
    void *handle = NULL;
    handle = dlopen("./libprintint.so", RTLD_LAZY);
    if (handle == NULL ) {
        // use dlerror to find out what went wrong
        return -1;
    }
    printint *sym = NULL;
    sym = (printint*)dlsym(handle, "printint");
    if (sym == NULL ) {
        // use dlerror to find out what went wrong
        return -1;
    }
    sym(argc);
    dlclose(handle);
    return 0;
}
--------------

You compile this program like this:

              gcc  main.c -ldl -o a.out

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.



> Waiting for your advises,
> Lev.
>
> _______________________________________________
> Linux-il mailing list
> Linux-il@cs.huji.ac.il
> http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il
>

[-- Attachment #1.2: Type: text/html, Size: 5688 bytes --]

<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 &lt;<a href="mailto:levonshe@yandex.com">levonshe@yandex.com</a>&gt; 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>      &quot;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&#39;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.&quot;</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&#39;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&#39;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 &lt;stdio.h&gt;</div><div><br></div><div>void printint(int num) {</div><div>    printf(&quot;Called with num=%d \n&quot;, 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 &lt;dlfcn.h&gt;<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(&quot;./libprintint.so&quot;, 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, &quot;printint&quot;);<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&#39;t dynamically link to libprintint.so by running &quot;ldd ./a.out&quot;. 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>

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to signal kernel that shared library is not in use by any process anymore ?
  2018-12-21 14:20 How to signal kernel that shared library is not in use by any process anymore ? Lev Olshvang
  2018-12-21 15:31 ` Greg KH
  2018-12-21 16:37 ` Lior Okman
@ 2018-12-22  2:34 ` Bernd Petrovitsch
  2 siblings, 0 replies; 7+ messages in thread
From: Bernd Petrovitsch @ 2018-12-22  2:34 UTC (permalink / raw)
  To: Lev Olshvang, kernelnewbies, linux-il

Hi all!

On 21/12/2018 15:20, Lev Olshvang wrote:
[...]
> I have  an executable (C++) which is the exclusive user of the some shared library that it uses only during  the initialization phase.
> 
> I would like to free memory used by this shared library, because I am running on embedded system.
> 
> How can I achieve this?
> 
> I know that dlopen() will load shared library, and hope that following dlclose() will free this lib memory. 
> 
> 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 ?

There is no such method TTBOMK. Since the linker (and thus the shared
loader) do not know of this library, they can't do anything (see below).

The C++ application has to resolve the symbols with dlsym() (after a
successful dlopen()) and call it with an appropriate type cast.
After dlclose(), the pointers from dlasym() are invalid (obviously - as
the lib is gone/unmapped).

> 2. And  how to tell the dynamic linker ld-linux.so to postpone the symbol resolution until dlopen()  will load  the library?

Do not mention that library (when linking the application).

> 3. Whether to compile and link executable with this library or leave unresolved symbols?

You do not call the functions directly (and you cannot as the linker
cannot resolve them anyways as the linker doesn't know the library).

MfG,
	Bernd
-- 
Bernd Petrovitsch                  Email : bernd@petrovitsch.priv.at
                     LUGA : http://www.luga.at

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to signal kernel that shared library is not in use by any process anymore ?
  2018-12-21 15:31 ` Greg KH
@ 2018-12-27  7:47   ` Lev Olshvang
  2018-12-27  9:43     ` Greg KH
  0 siblings, 1 reply; 7+ messages in thread
From: Lev Olshvang @ 2018-12-27  7:47 UTC (permalink / raw)
  To: Greg KH; +Cc: linux-il, kernelnewbies

Hello Greg,

Thanks for you your reply.
It help me to better express my question

From the application I can access /proc/self/maps and see which memory is  mapped  for my library I do not intend to use after application passes init phase.
I would like to unmap this memory region, but since I do not have file descriptor for this so I can not do this.
Only kernel can unmap this memory for me,
I am willing to pass library name or address to system cal, but IMHO such system call does not exist.
Should I rely on memory manager which will use reuse this pages because they will never page faulted back ?

I am afraid that since this is C++ lib, some pages are modified and will not be considered clean ?


Thanks again for your precious time.
Perhaps somebody from the list can respond too.

Thank you all
Lev

21.12.2018, 18:31, "Greg KH" <greg@kroah.com>:
> On Fri, Dec 21, 2018 at 05:20:36PM +0300, Lev Olshvang wrote:
>>  Hi All,
>>
>>  I have an executable (C++) which is the exclusive user of the some shared library that it uses only during the initialization phase.
>>
>>  I would like to free memory used by this shared library, because I am running on embedded system.
>>
>>  How can I achieve this?
>>
>>  I know that dlopen() will load shared library, and hope that following dlclose() will free this lib memory.
>
> That right there is how you "achieve" this, call dlclose() and all will
> be fine. If your system needs the memory that was being used, it will
> reclaim it from the memory that was previously being used by the library
> at this point in time.
>
> Nothing else needs to be done.
>
> Have you tested this and found it not to work properly?
>
>>  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 ?
>
> What "newly appeared symbols"?
>
> If you need to load the library again, call dlopen().
>
>>  2. And how to tell the dynamic linker ld-linux.so to postpone the symbol resolution until dlopen() will load the library?
>
> It will not happen until you tell it to, right?
>
>>  3. Whether to compile and link executable with this library or leave unresolved symbols?
>
> It sounds like you don't really know what type of problem you are trying
> to solve here.
>
> Back up, what is the real issue you are having with the kernel at this
> point in time?
>
> thanks,
>
> greg k-h

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to signal kernel that shared library is not in use by any process anymore ?
  2018-12-27  7:47   ` Lev Olshvang
@ 2018-12-27  9:43     ` Greg KH
  0 siblings, 0 replies; 7+ messages in thread
From: Greg KH @ 2018-12-27  9:43 UTC (permalink / raw)
  To: Lev Olshvang; +Cc: linux-il, kernelnewbies

On Thu, Dec 27, 2018 at 10:47:42AM +0300, Lev Olshvang wrote:
> Hello Greg,
> 
> Thanks for you your reply.
> It help me to better express my question
> 
> >From the application I can access /proc/self/maps and see which memory is  mapped  for my library I do not intend to use after application passes init phase.
> I would like to unmap this memory region, but since I do not have file descriptor for this so I can not do this.

Why do you care?  The loader should handle all of this for you
automatically.

> Only kernel can unmap this memory for me,
> I am willing to pass library name or address to system cal, but IMHO such system call does not exist.
> Should I rely on memory manager which will use reuse this pages because they will never page faulted back ?

Yes you should.

> I am afraid that since this is C++ lib, some pages are modified and will not be considered clean ?

That depends on your code, if it is still in use, yes, it will not be
freed, you have control over that as this is your userspace code.  There
is nothing in the kernel you can do about this, sorry, please just fix
up your application.

good luck!

greg k-h

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to signal kernel that shared library is not in use by any process anymore ?
  2018-12-21 16:37 ` Lior Okman
@ 2019-01-08 15:56   ` Lev Olshvang
  0 siblings, 0 replies; 7+ messages in thread
From: Lev Olshvang @ 2019-01-08 15:56 UTC (permalink / raw)
  To: Lior Okman; +Cc: linux-il, kernelnewbies

[-- Attachment #1: Type: text/html, Size: 7441 bytes --]

<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" &lt;lior@okman.name&gt;:</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 &lt;<a rel="noopener noreferrer" href="mailto:levonshe@yandex.com">levonshe@yandex.com</a>&gt; 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 &lt;stdio.h&gt;</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 &lt;dlfcn.h&gt;</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>

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, back to index

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-21 14:20 How to signal kernel that shared library is not in use by any process anymore ? Lev Olshvang
2018-12-21 15:31 ` Greg KH
2018-12-27  7:47   ` Lev Olshvang
2018-12-27  9:43     ` Greg KH
2018-12-21 16:37 ` Lior Okman
2019-01-08 15:56   ` Lev Olshvang
2018-12-22  2:34 ` Bernd Petrovitsch

Kernel Newbies archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/kernelnewbies/0 kernelnewbies/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kernelnewbies kernelnewbies/ https://lore.kernel.org/kernelnewbies \
		kernelnewbies@kernelnewbies.org kernelnewbies@archiver.kernel.org
	public-inbox-index kernelnewbies


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernelnewbies.kernelnewbies


AGPL code for this site: git clone https://public-inbox.org/ public-inbox