How can I use .init and .finit section of ELF shared object?

E

Ender.Dai

I have writen following demo code, but it doesn't work :(

Source code:
--------------------------------
/* hello.c */
#include <stdio.h>
extern int hello_init() __attribute__ ((section(".init")));
extern int hello_finit() __attribute__ ((section(".finit")));

int hello_init()
{
printf("init hello\n");
return 0;
}

int hello()
{
printf("hello world\n");
return 0;
}

int hello_finit()
{
printf("finit hello\n");
return 0;
}


/* main.c */
int hello();
int main()
{
hello();
return 0;
}


Test result:
----------------------
ender@localhost ~/tmp
$ gcc -shared -Wl,-soname,libhw.so.1 -fPIC hello.c -o libhw.so.1

root@localhost ~/tmp
$ ln -s libhw.so.1 libhw.so

ender@localhost ~/tmp
$ gcc -L. -lhw main.c

ender@localhost ~/tmp
$ LD_LIBRARY_PATH=. ./a.out
init hello
Segmentation fault


Damn it, "Segmentation fault". Thanks for goole, I found this page
later,

http://www.flipcode.com/cgi-bin/fcarticles.cgi?show=63896

and then rewrite my hello.c:


#include <stdio.h>

int hello_init()
{
__asm__ (".section .init \n call hello_init \n .section .text\n");
printf("init hello\n");
return 0;
}

int hello()
{
printf("hello world\n");
return 0;
}

int hello_finit()
{
__asm__ (".section .finit \n call hello_finit \n .section .text\n");
printf("finit hello\n");
return 0;
}

And this is the test result:
---------------------------------------
ender@localhost ~/tmp
$ gcc -shared -Wl,-soname,libhw.so.1 -fPIC hello.c -o libhw.so.1

ender@localhost ~/tmp
$ LD_LIBRARY_PATH=. ./a.out
init hello
hello world

It won't segmentation fault now, but where is the hello_fini()? Seems
that it didn't run.

Could anybody tell my why, or show me a piece of demo code? Any telp
will be appreciated.
 
D

d3x0xr

I find that __attribute__((destructor)) and __attribute__((constructor))
works better. finit and init were depricated?

Ender.Dai:
 
E

Ender.Dai

Thanks for your kindly reply, but seems like that neither hello_init()
nor hello_finit() will be called if we use __attribute__((destructor))
and __attribute__((constructor)).
 
I

Ian Collins

Ender.Dai said:
I have writen following demo code, but it doesn't work :(

Source code:
--------------------------------
/* hello.c */
#include <stdio.h>
extern int hello_init() __attribute__ ((section(".init")));
extern int hello_finit() __attribute__ ((section(".finit")));
All rather OT here, try a gcc group.
 
D

d3x0xr

I have no problem having this code work

$ gcc -o test test.c
test.c: In function 'preinit':
test.c:4: warning: incompatible implicit declaration of built-in
function 'printf'
test.c: In function 'postinit':
test.c:9: warning: incompatible implicit declaration of built-in
function 'printf'
test.c: In function 'main':
test.c:13: warning: incompatible implicit declaration of built-in
function 'printf'
root@tower:/tmp/zz
$ ./test
before main
in main
after main
root@tower:/tmp/zz
$ cat test.c
void preinit( void ) __attribute__((constructor));
void preinit( void )
{
printf( "before main\n" );
}
void postinit( void ) __attribute__((destructor));
void postinit( void )
{
printf( "after main\n" );
}
int main( void )
{
printf( "in main\n" );
return 0;
}

Ender.Dai:
 
K

Keith Thompson

Ender.Dai said:
I have writen following demo code, but it doesn't work :(

Source code:
--------------------------------
/* hello.c */
#include <stdio.h>
extern int hello_init() __attribute__ ((section(".init")));
extern int hello_finit() __attribute__ ((section(".finit")));
[...]

This isn't defined by the C language. Try gnu.gcc.help.
 
D

Default User

Ender.Dai said:
Thanks for your kindly reply, but seems like that neither hello_init()
nor hello_finit() will be called if we use attribute((destructor))
and attribute((constructor)).

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top