Entry and exit function in a shared object

Do shared objects have an entry and exit function that get called when they
are loaded and unloaded by the OS? I would like to be able to have some
code execute when my shared object is being unloaded by the OS. Windows
DLLs have this feature. Thanks!

Robert

QNX Newsgroup <brecknr@intgame.com> wrote:

Do shared objects have an entry and exit function that get called when they
are loaded and unloaded by the OS? I would like to be able to have some
code execute when my shared object is being unloaded by the OS. Windows
DLLs have this feature. Thanks!

Try this (not guaranteed to work on all CPU platforms)

#include <stdio.h>
#include <unistd.h>

static void init_aux(void)
{
printf(“init_aux!\n”);
}
static void init_dummy(void)
{
asm( “.section\t.init”);
init_aux();
asm( “.text”);
}
static void cleanup_aux(void)
{
printf(“cleanup_aux!\n”);
}
static void cleanup_dummy(void)
{
asm( “.section\t.fini”);
cleanup_aux();
asm( “.text”);
}


cburgess@qnx.com

That does work except for one case (which is the case I was looking to
cover). If for some reason there is a crash (sigsegv), this function is not
called. Any other ideas?

Robert


“Colin Burgess” <cburgess@qnx.com> wrote in message
news:99lkvd$5ln$3@nntp.qnx.com

QNX Newsgroup <> brecknr@intgame.com> > wrote:
Do shared objects have an entry and exit function that get called when
they
are loaded and unloaded by the OS? I would like to be able to have some
code execute when my shared object is being unloaded by the OS. Windows
DLLs have this feature. Thanks!

Try this (not guaranteed to work on all CPU platforms)

#include <stdio.h
#include <unistd.h

static void init_aux(void)
{
printf(“init_aux!\n”);
}
static void init_dummy(void)
{
asm( “.section\t.init”);
init_aux();
asm( “.text”);
}
static void cleanup_aux(void)
{
printf(“cleanup_aux!\n”);
}
static void cleanup_dummy(void)
{
asm( “.section\t.fini”);
cleanup_aux();
asm( “.text”);
}


cburgess@qnx.com

QNX Newsgroup <brecknr@intgame.com> wrote:

That does work except for one case (which is the case I was looking to
cover). If for some reason there is a crash (sigsegv), this function is not
called. Any other ideas?

You will have to register a SIGSEGV handler, call your exit function, then
exit the program.

Robert



“Colin Burgess” <> cburgess@qnx.com> > wrote in message
news:99lkvd$5ln$> 3@nntp.qnx.com> …
QNX Newsgroup <> brecknr@intgame.com> > wrote:
Do shared objects have an entry and exit function that get called when
they
are loaded and unloaded by the OS? I would like to be able to have some
code execute when my shared object is being unloaded by the OS. Windows
DLLs have this feature. Thanks!

Try this (not guaranteed to work on all CPU platforms)

#include <stdio.h
#include <unistd.h

static void init_aux(void)
{
printf(“init_aux!\n”);
}
static void init_dummy(void)
{
asm( “.section\t.init”);
init_aux();
asm( “.text”);
}
static void cleanup_aux(void)
{
printf(“cleanup_aux!\n”);
}
static void cleanup_dummy(void)
{
asm( “.section\t.fini”);
cleanup_aux();
asm( “.text”);
}


cburgess@qnx.com


cburgess@qnx.com