* Added the "c_gen.pl" perl script into this directory, so the current
[binutils-gdb.git] / ld / ldctor.c
blob8ba866c88e40b354468e61ad113a684779e60b9d
1 /* ldctor.c -- constructor support routines
2 Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc.
3 By Steve Chamberlain <sac@cygnus.com>
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
25 #include "ld.h"
26 #include "ldexp.h"
27 #include "ldlang.h"
28 #include "ldmisc.h"
29 #include "ldgram.h"
30 #include "ldmain.h"
31 #include "ldctor.h"
33 /* The list of statements needed to handle constructors. These are
34 invoked by the command CONSTRUCTORS in the linker script. */
35 lang_statement_list_type constructor_list;
37 /* We keep a list of these structures for each sets we build. */
39 struct set_info
41 struct set_info *next; /* Next set. */
42 struct bfd_link_hash_entry *h; /* Hash table entry. */
43 bfd_reloc_code_real_type reloc; /* Reloc to use for an entry. */
44 size_t count; /* Number of elements. */
45 struct set_element *elements; /* Elements in set. */
48 struct set_element
50 struct set_element *next; /* Next element. */
51 asection *section; /* Section of value. */
52 bfd_vma value; /* Value. */
55 /* The sets we have seen. */
57 static struct set_info *sets;
59 /* Add an entry to a set. H is the entry in the linker hash table.
60 RELOC is the relocation to use for an entry in the set. SECTION
61 and VALUE are the value to add. This is called during the first
62 phase of the link, when we are still gathering symbols together.
63 We just record the information now. The ldctor_find_constructors
64 function will construct the sets. */
66 void
67 ldctor_add_set_entry (h, reloc, section, value)
68 struct bfd_link_hash_entry *h;
69 bfd_reloc_code_real_type reloc;
70 asection *section;
71 bfd_vma value;
73 struct set_info *p;
74 struct set_element *e;
75 struct set_element **epp;
77 for (p = sets; p != (struct set_info *) NULL; p = p->next)
78 if (p->h == h)
79 break;
81 if (p == (struct set_info *) NULL)
83 p = (struct set_info *) xmalloc (sizeof (struct set_info));
84 p->next = sets;
85 sets = p;
86 p->h = h;
87 p->reloc = reloc;
88 p->count = 0;
89 p->elements = NULL;
91 else
93 if (p->reloc != reloc)
95 einfo ("%P%X: Different relocs used in set %s\n", h->root.string);
96 return;
99 /* Don't permit a set to be constructed from different object
100 file formats. The same reloc may have different results. We
101 actually could sometimes handle this, but the case is
102 unlikely to ever arise. Sometimes constructor symbols are in
103 unusual sections, such as the absolute section--this appears
104 to be the case in Linux a.out--and in such cases we just
105 assume everything is OK. */
106 if (p->elements != NULL
107 && section->owner != NULL
108 && p->elements->section->owner != NULL
109 && strcmp (bfd_get_target (section->owner),
110 bfd_get_target (p->elements->section->owner)) != 0)
112 einfo ("%P%X: Different object file formats composing set %s\n",
113 h->root.string);
114 return;
118 e = (struct set_element *) xmalloc (sizeof (struct set_element));
119 e->next = NULL;
120 e->section = section;
121 e->value = value;
123 for (epp = &p->elements; *epp != NULL; epp = &(*epp)->next)
125 *epp = e;
127 ++p->count;
130 /* This function is called after the first phase of the link and
131 before the second phase. At this point all set information has
132 been gathered. We now put the statements to build the sets
133 themselves into constructor_list. */
135 void
136 ldctor_build_sets ()
138 lang_statement_list_type *old;
139 struct set_info *p;
141 old = stat_ptr;
142 stat_ptr = &constructor_list;
144 lang_list_init (stat_ptr);
146 for (p = sets; p != (struct set_info *) NULL; p = p->next)
148 struct set_element *e;
149 reloc_howto_type *howto;
150 int size;
152 /* If the symbol is defined, we may have been invoked from
153 collect, and the sets may already have been built, so we do
154 not do anything. */
155 if (p->h->type == bfd_link_hash_defined)
156 continue;
158 /* For each set we build:
159 set:
160 .long number_of_elements
161 .long element0
163 .long elementN
164 .long 0
165 except that we use the right size instead of .long. When
166 generating relocateable output, we generate relocs instead of
167 addresses. */
168 howto = bfd_reloc_type_lookup (output_bfd, p->reloc);
169 if (howto == (reloc_howto_type *) NULL)
171 if (link_info.relocateable)
173 einfo ("%P%X: %s does not support reloc %s for set %s\n",
174 bfd_get_target (output_bfd),
175 bfd_get_reloc_code_name (p->reloc),
176 p->h->root.string);
177 continue;
180 /* If this is not a relocateable link, all we need is the
181 size, which we can get from the input BFD. */
182 howto = bfd_reloc_type_lookup (p->elements->section->owner,
183 p->reloc);
184 if (howto == NULL)
186 einfo ("%P%X: %s does not support reloc %s for set %s\n",
187 bfd_get_target (p->elements->section->owner),
188 bfd_get_reloc_code_name (p->reloc),
189 p->h->root.string);
190 continue;
194 switch (bfd_get_reloc_size (howto))
196 case 1: size = BYTE; break;
197 case 2: size = SHORT; break;
198 case 4: size = LONG; break;
199 case 8: size = QUAD; break;
200 default:
201 einfo ("%P%X: Unsupported size %d for set %s\n",
202 bfd_get_reloc_size (howto), p->h->root.string);
203 size = LONG;
204 break;
207 lang_add_assignment (exp_assop ('=', p->h->root.string,
208 exp_nameop (NAME, ".")));
209 lang_add_data (size, exp_intop ((bfd_vma) p->count));
211 for (e = p->elements; e != (struct set_element *) NULL; e = e->next)
213 if (link_info.relocateable)
214 lang_add_reloc (p->reloc, howto, e->section,
215 (const char *) NULL, exp_intop (e->value));
216 else
217 lang_add_data (size, exp_relop (e->section, e->value));
220 lang_add_data (size, exp_intop (0));
223 stat_ptr = old;