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