Even more documentation
[deliverable/binutils-gdb.git] / bfd / ctor.c
CommitLineData
e5e193c7
SC
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
8This file is part of BFD, the Binary File Descriptor library.
9
10This program is free software; you can redistribute it and/or modify
11it under the terms of the GNU General Public License as published by
12the Free Software Foundation; either version 2 of the License, or
13(at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23
24/*doc*
25@section Constructors
26Classes in C++ have 'constructors' and 'destructors'. These are
27functions which are called automatically by the language whenever data
28of a class is created or destroyed. Class data which is static data
29may also be have a type which requires 'construction', the contructor
30must be called before the data can be referenced, so the contructor
31must be called before the program begins.
32
33The common solution to this problem is for the compiler to call a
34magic function as the first statement @code{main}. This magic
35function, (often called @code{__main}) runs around calling the
36constructors for all the things needing it.
37
38With COFF the compile has a bargain with the linker et al. All
39constructors are given strange names, for example
40@code{__GLOBAL__$I$foo} might be the label of a contructor for the
41class @var{foo}. The solution on unfortunate systems (most system V
42machines) 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
44looking for strange @code{__GLOBAL__$} symbols, generate a C program
45from this, compile it and link with the partially linked input. This
46process is usually called @code{collect}.
47
48Some versions of @code{a.out} use something called the
49@code{set_vector} mechanism. The constructor symbols are output from
50the compiler with a special stab code saying that they are
51constructors, and the linker can deal with them directly.
52
53BFD allows applications (ie the linker) to deal with constructor
54information independently of their external implimentation by
55providing a set of entry points for the indiviual object back ends to
56call which maintains a database of the contructor information. The
57application can interrogate the database to find out what it wants.
58
59The construction data essential for the linker to be able to perform
60its job are:
61
62@itemize @bullet
63@item asymbol
64The asymbol of the contructor entry point contains all the information
65necessary to call the function.
66@item table id
67The type of symbol, ie is it a contructor, a destructor or something
68else someone dreamed up to make our lives difficult.
69@end itemize
70
71This module takes this information and then builds extra sections
72attached to the bfds which own the entry points. It creates these
73sections as if they were tables of pointers to the entry points, and
74builds relocation entries to go with them so that the tables can be
75relocated along with the data they reference.
76
77These sections are marked with a special bit (@code{SEC_CONSTRUCTOR})
78which 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
91This function is called with an a symbol describing the
92function to be called, an string which descibes the xtor type, eg
93something like "CTOR" or "DTOR" would be fine. And the bfd which owns
94the function.
95
96It's duty is to create a section called "CTOR" or "DTOR" or whatever
97if the bfd doesn't already have one, and grow a relocation table for
98the entry points as they accumulate.
99
100
101*; PROTO(void, bfd_constructor_entry,
102 (bfd *abfd,
103 asymbol **symbol_ptr_ptr,
522e0ead 104 CONST char*type));
e5e193c7
SC
105
106*/
107
108
109void 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}
This page took 0.031291 seconds and 4 git commands to generate.