Initial revision
[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 /*doc*
25 @section Constructors
26 Classes in C++ have 'constructors' and 'destructors'. These are
27 functions which are called automatically by the language whenever data
28 of a class is created or destroyed. Class data which is static data
29 may also be have a type which requires 'construction', the contructor
30 must be called before the data can be referenced, so the contructor
31 must be called before the program begins.
32
33 The common solution to this problem is for the compiler to call a
34 magic function as the first statement @code{main}. This magic
35 function, (often called @code{__main}) runs around calling the
36 constructors for all the things needing it.
37
38 With COFF the compile has a bargain with the linker et al. All
39 constructors are given strange names, for example
40 @code{__GLOBAL__$I$foo} might be the label of a contructor for the
41 class @var{foo}. The solution on unfortunate systems (most system V
42 machines) is to perform a partial link on all the .o files, do an
43 @code{nm} on the result, run @code{awk} or some such over the result
44 looking for strange @code{__GLOBAL__$} symbols, generate a C program
45 from this, compile it and link with the partially linked input. This
46 process is usually called @code{collect}.
47
48 Some versions of @code{a.out} use something called the
49 @code{set_vector} mechanism. The constructor symbols are output from
50 the compiler with a special stab code saying that they are
51 constructors, and the linker can deal with them directly.
52
53 BFD allows applications (ie the linker) to deal with constructor
54 information independently of their external implimentation by
55 providing a set of entry points for the indiviual object back ends to
56 call which maintains a database of the contructor information. The
57 application can interrogate the database to find out what it wants.
58
59 The construction data essential for the linker to be able to perform
60 its job are:
61
62 @itemize @bullet
63 @item asymbol
64 The asymbol of the contructor entry point contains all the information
65 necessary to call the function.
66 @item table id
67 The type of symbol, ie is it a contructor, a destructor or something
68 else someone dreamed up to make our lives difficult.
69 @end itemize
70
71 This module takes this information and then builds extra sections
72 attached to the bfds which own the entry points. It creates these
73 sections as if they were tables of pointers to the entry points, and
74 builds relocation entries to go with them so that the tables can be
75 relocated along with the data they reference.
76
77 These sections are marked with a special bit (@code{SEC_CONSTRUCTOR})
78 which the linker notices and do with what it wants.
79
80
81 */
82
83 #include <bfd.h>
84 #include <sysdep.h>
85 #include <libbfd.h>
86
87
88
89 /*proto-internal* bfd_constructor_entry
90
91 This function is called with an a symbol describing the
92 function to be called, an string which descibes the xtor type, eg
93 something like "CTOR" or "DTOR" would be fine. And the bfd which owns
94 the function.
95
96 It's duty is to create a section called "CTOR" or "DTOR" or whatever
97 if the bfd doesn't already have one, and grow a relocation table for
98 the entry points as they accumulate.
99
100
101 *; PROTO(void, bfd_constructor_entry,
102 (bfd *abfd,
103 asymbol **symbol_ptr_ptr,
104 CONST char*type);
105
106 */
107
108
109 void DEFUN(bfd_constructor_entry,(abfd, symbol_ptr_ptr, type),
110 bfd *abfd AND
111 asymbol **symbol_ptr_ptr AND
112 CONST char *type)
113
114 {
115 /* Look up the section we're using to store the table in */
116 asection *rel_section = bfd_get_section_by_name (abfd, type);
117 if (rel_section == (asection *)NULL) {
118 rel_section = bfd_make_section (abfd, type);
119 rel_section->flags = SEC_CONSTRUCTOR;
120 rel_section->alignment_power = 2;
121 }
122
123 /* Create a relocation into the section which references the entry
124 point */
125 {
126 arelent_chain *reloc = (arelent_chain *)bfd_alloc(abfd,
127 sizeof(arelent_chain));
128
129 reloc->relent.section = (asection *)NULL;
130 reloc->relent.addend = 0;
131
132 reloc->relent.sym_ptr_ptr = symbol_ptr_ptr;
133 reloc->next = rel_section->constructor_chain;
134 rel_section->constructor_chain = reloc;
135 reloc->relent.address = rel_section->size;
136 /* ask the cpu which howto to use */
137 reloc->relent.howto =
138 bfd_reloc_type_lookup(abfd->arch_info,
139 BFD_RELOC_CTOR);
140 rel_section->size += sizeof(int *);
141 rel_section->reloc_count++;
142 }
143
144 }