necessity of .init section !!

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

necessity of .init section !!

ashwani raina
Hi,
     Is it necessary to have .init section in an ELF ( especially
shared object) ?
Because I have seen the ELF which has no .init section ? Also is it
possible to have a my code in .init section which should be executed
before control goes to main ..because i wanted to debug my executable.

--
regards
ashwani
---
It is by logic that we prove but by intuition that we discover.......
Reply | Threaded
Open this post in threaded view
|

Re: necessity of .init section !!

Ian Lance Taylor
ashwani raina <[hidden email]> writes:

>      Is it necessary to have .init section in an ELF ( especially
> shared object) ?

The .init section is not necessary.  There is nothing special about
the .init section.

> Because I have seen the ELF which has no .init section ? Also is it
> possible to have a my code in .init section which should be executed
> before control goes to main ..because i wanted to debug my executable.

To have code executed before main, you want to set a DT_INIT,
DT_INIT_ARRAY or DT_PREINIT_ARRAY entry in the dynamic tags (if you
are doing a static link, there is no way to force code to be executed
before main; DT_PREINIT_ARRAY is only permitted in an executable, not
in a shared library).  The dynamic linker ld.so will look for DT_INIT,
DT_INIT_ARRAY, or DT_PREINIT_ARRAY and invoke the code to which it
points.

By default the GNU linker will set DT_INIT to the address of a
function named "_init", if there is one.  See the -init option.  And
by default the GNU linker will set DT_INIT_ARRAY to the address of a
section named ".init_array", if there is one.

Ian