trying to understand the effect of the -init/-fini option

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

trying to understand the effect of the -init/-fini option

shankarke@gmail.com
I am trying to understand the effect of the -init/-fini command line
options. I was thinking that these options create an .init section/.fini
section and call the functions specified in the command line when the binary
is initialized / atexit time.

Below is an example that I am working with :-

1.c
----
int fn() {
printf("Hello world\n");
return 0;
}

int main() { return 0; }

$gcc --version
gcc (Ubuntu 4.8.1-2ubuntu1~10.04.1) 4.8.1

Case (1)
------------
$gcc 1.c -Wl,-init,fn -o out
$./out ==> Expect to print "Hello World"
(Nothing is printed)

Case (2)
-----------
$cat 1.c
int fn() {
return 0;
}

int main() { return 0; }

$gcc -c 1.c
$ld 1.o -init fn -fini fn -o out
$readelf -S -W out | grep -E 'init|fini'

(Nothing is printed)

I expected to see the linker to create init/fini sections.

Case (3)
-----------
$cat 1.c
int fn() {
return 0;
}
$gcc -c 1.c && ar cr lib1.a 1.o
$ld -init fn lib1.a -o out -t
(Nothing is printed)

Expected 1.o to be pulled in from the archive and the linker to create
init/fini sections.

Case (4)
-----------
What is the difference between .init section and .init_array section ?

When does the linker try to create either of them ?

Similiar is the question of the difference between the .fini section and the
.fini_array section ?

Case (5)
-------------
Is there a difference when a shared object is created for the above cases as
I see the linker is setting DT_INIT/DT_FINI to point to the functions that I
specify for -init/-fini respectively.


Thanks

Shankar Easwaran