New documentation style
[binutils-gdb.git] / bfd / ctor.c
1 /* BFD library support routines for constructors
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3
4 Hacked by Steve Chamberlain of Cygnus Support. With some help from
5 Judy Chamberlain too.
6
7
8 This file is part of BFD, the Binary File Descriptor library.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23
24 /*
25 SECTION
26 Constructors
27
28 DESCRIPTION
29 Classes in C++ have 'constructors' and 'destructors'. These
30 are functions which are called automatically by the language
31 whenever data of a class is created or destroyed. Class data
32 which is static data may also be have a type which requires
33 'construction', the contructor must be called before the data
34 can be referenced, so the contructor must be called before the
35 program begins.
36
37 The common solution to this problem is for the compiler to
38 call a magic function as the first statement <<main>>.
39 This magic function, (often called <<__main>>) runs around
40 calling the constructors for all the things needing it.
41
42 With COFF the compile has a bargain with the linker et al.
43 All constructors are given strange names, for example
44 <<__GLOBAL__$I$foo>> might be the label of a contructor for
45 the class @var{foo}. The solution on unfortunate systems
46 (most system V machines) is to perform a partial link on all
47 the .o files, do an <<nm>> on the result, run <<awk>> or some
48 such over the result looking for strange <<__GLOBAL__$>>
49 symbols, generate a C program from this, compile it and link
50 with the partially linked input. This process is usually
51 called <<collect>>.
52
53 Some versions of <<a.out>> use something called the
54 <<set_vector>> mechanism. The constructor symbols are output
55 from the compiler with a special stab code saying that they
56 are constructors, and the linker can deal with them directly.
57
58 BFD allows applications (ie the linker) to deal with
59 constructor information independently of their external
60 implimentation by providing a set of entry points for the
61 indiviual object back ends to call which maintains a database
62 of the contructor information. The application can
63 interrogate the database to find out what it wants. The
64 construction data essential for the linker to be able to
65 perform its job are:
66
67 o asymbol
68 The asymbol of the contructor entry point contains all the
69 information necessary to call the function.
70
71 o table id
72 The type of symbol, ie is it a contructor, a destructor or
73 something else someone dreamed up to make our lives difficult.
74
75 This module takes this information and then builds extra
76 sections attached to the bfds which own the entry points. It
77 creates these sections as if they were tables of pointers to
78 the entry points, and builds relocation entries to go with
79 them so that the tables can be relocated along with the data
80 they reference.
81
82 These sections are marked with a special bit
83 (<<SEC_CONSTRUCTOR>>) which the linker notices and do with
84 what it wants.
85
86 */
87
88 #include <bfd.h>
89 #include <sysdep.h>
90 #include <libbfd.h>
91
92
93
94 /*
95 INTERNAL FUNCTION
96 bfd_constructor_entry
97
98 DESCRIPTION
99 This function is called with an a symbol describing the
100 function to be called, an string which descibes the xtor type,
101 eg something like "CTOR" or "DTOR" would be fine. And the bfd
102 which owns the function. Its duty is to create a section
103 called "CTOR" or "DTOR" or whatever if the bfd doesn't already
104 have one, and grow a relocation table for the entry points as
105 they accumulate.
106
107 SYNOPSIS
108 void bfd_constructor_entry(bfd *abfd,
109 asymbol **symbol_ptr_ptr,
110 CONST char*type);
111
112 */
113
114
115 void DEFUN(bfd_constructor_entry,(abfd, symbol_ptr_ptr, type),
116 bfd *abfd AND
117 asymbol **symbol_ptr_ptr AND
118 CONST char *type)
119
120 {
121 /* Look up the section we're using to store the table in */
122 asection *rel_section = bfd_get_section_by_name (abfd, type);
123 if (rel_section == (asection *)NULL) {
124 rel_section = bfd_make_section (abfd, type);
125 rel_section->flags = SEC_CONSTRUCTOR;
126 rel_section->alignment_power = 2;
127 }
128
129 /* Create a relocation into the section which references the entry
130 point */
131 {
132 arelent_chain *reloc = (arelent_chain *)bfd_alloc(abfd,
133 sizeof(arelent_chain));
134
135 reloc->relent.section = (asection *)NULL;
136 reloc->relent.addend = 0;
137
138 reloc->relent.sym_ptr_ptr = symbol_ptr_ptr;
139 reloc->next = rel_section->constructor_chain;
140 rel_section->constructor_chain = reloc;
141 reloc->relent.address = rel_section->size;
142 /* ask the cpu which howto to use */
143 reloc->relent.howto =
144 bfd_reloc_type_lookup(abfd->arch_info,
145 BFD_RELOC_CTOR);
146 rel_section->size += sizeof(int *);
147 rel_section->reloc_count++;
148 }
149
150 }