Re: Adding custom code to the .init section of a library

plakal@cs.wisc.edu
18 Nov 1999 02:48:44 -0500

          From comp.compilers

Related articles
Adding custom code to the .init section of a library priya@alpha.ece.ucsb.edu (Priya Narasimhan) (1999-11-16)
Re: Adding custom code to the .init section of a library fjh@cs.mu.OZ.AU (1999-11-18)
Re: Adding custom code to the .init section of a library mwh@gradin.cis.upenn.edu (1999-11-18)
Re: Adding custom code to the .init section of a library zandy@cs.wisc.edu (Victor Zandy) (1999-11-18)
Re: Adding custom code to the .init section of a library plakal@cs.wisc.edu (1999-11-18)
| List of all articles for this month |
From: plakal@cs.wisc.edu
Newsgroups: comp.compilers
Date: 18 Nov 1999 02:48:44 -0500
Organization: University of WI, Madison -- Computer Sciences Dept.
References: 99-11-082
Keywords: linker

Priya Narasimhan <priya@alpha.ece.ucsb.edu> wrote:
> On a SPARC Solaris 2.x machine, I am able to add custom code
> to the .init section of a library using
> #pragma init (blah-blah)
> where blah-blah() is written in C code. How can I do this on
> an Intel machine running RedHatLinux 6.0 with gcc? I tried the
> #pragma init, and it did not work. I really would like to add
>
> [I told him to use a C++ static constructor. -John]


If you're using C, you could write a small assembly
file that contains _init and _fini sections. You
can then link in this file when you create the
library. E.g.:


.section ".init"
.align 4
.global_init
_init:
<set up a stack frame>
call init_function_name
<clean up after proc call>
ret


You can repeat this with a fini section. And you will
have to go through the pain of figuring out the
x86 assembly to stick in before and after the
procedure call.


When building a shared library using C++ compilers,
it is generally not guaranteed that constructors of
global objects are called at the time that the
library is loaded (i.e., they may not be included
in the .init section of the library). At least, I saw
this when using egcs on SPARC/Solaris. The egcs FAQ
mentions that you can do some jugglery using the
collect2 linking tool to achieve this. Instead, I
simply made all my global objects of simple types
(int,pointer etc) which were initialized explicitly
within the library rather than relying on a constructor
being called at library-load time.


Manoj


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.