473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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

Nov 30 '06 #1
8 4437

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

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/fcar...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.
Nov 30 '06 #2
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)).

On Nov 30, 4:24 pm, "d3x0xr" <d...@nowhere.netwrote:
I find that __attribute__((destructor)) and __attribute__((constructor))
works better. finit and init were depricated?
Nov 30 '06 #3
Ender.Dai wrote:
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.

--
Ian Collins.
Nov 30 '06 #4
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:
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)).

On Nov 30, 4:24 pm, "d3x0xr" <d...@nowhere.netwrote:
>I find that __attribute__((destructor)) and
__attribute__((constructor))
>works better. finit and init were depricated?
Nov 30 '06 #5
"Ender.Dai" <ol*****@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 30 '06 #6

On Nov 30, 5:29 pm, Keith Thompson <k...@mib.orgwrote:
>
This isn't defined by the C language. Try gnu.gcc.help.
I'm sorry for the OT, and thanks for the hint, I'll move the question
to gnu.gcc.help.

Nov 30 '06 #7
d3x0xr wrote:
>
I find that attribute((destructor)) and attribute((constructor))
works better. finit and init were depricated?
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>
Nov 30 '06 #8
Ender.Dai wrote:
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>
Nov 30 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: LaBird | last post by:
Dear all, I'd like to ask for the following C code segment: #include <stdio.h> int main() { char *a = "abc"; /* Line 1 */ printf("%s", a); /* Line 2 */ a = '1'; /* Line...
3
by: paulw | last post by:
Hi I have a question: main() { static int i; printf("%d\n",i); // should I see see 0 or 5 ??? for (i=5;i<=15;i++) {...} // What's the meaning of static variable in
4
by: Halcyon Woodward | last post by:
I have an odd problem... We have a small development team (three coders) working on the same project (a C# web application). Each coder has a unique 'sandbox' site on a shared Windows 2003...
2
by: Christian | last post by:
I have a class named AccessClass to access a legacy system. In a webform i instanciate an AccessClass object named myAccess and i call a method : myAccess.accessmethod() I want to insure that no...
10
by: Wylbur via DotNetMonster.com | last post by:
Hello to all of you geniuses, I'm having a problem trying to get an Init handler to fire for a Placeholder control at the initialization phase. I’ve posted this problem to 3 other ASP.NET...
0
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
3
by: rayuthar | last post by:
Hi, I need to trigger a function, whenever my shared module (.so) gets loaded and before its been used by application. Any suggestions? Thanks Rayuthar
2
by: Christof Warlich | last post by:
Hi, I'm working on a (template) library that is up to now entirely implemented in header-files. This makes the library quite convenient to use as no extra object code needs to be linked when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.