]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/ldctor.c
* ldgram.y: Include "ldctor.h".
[thirdparty/binutils-gdb.git] / ld / ldctor.c
1 /* ldctor.c -- constructor support routines
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
4 By Steve Chamberlain <sac@cygnus.com>
5
6 This file is part of GLD, the Gnu Linker.
7
8 GLD is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GLD is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GLD; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "bfdlink.h"
26
27 #include <ctype.h>
28
29 #include "ld.h"
30 #include "ldexp.h"
31 #include "ldlang.h"
32 #include "ldmisc.h"
33 #include "ldgram.h"
34 #include "ldmain.h"
35 #include "ldctor.h"
36
37 static int ctor_prio PARAMS ((const char *));
38 static int ctor_cmp PARAMS ((const PTR, const PTR));
39
40 /* The list of statements needed to handle constructors. These are
41 invoked by the command CONSTRUCTORS in the linker script. */
42 lang_statement_list_type constructor_list;
43
44 /* Whether the constructors should be sorted. Note that this is
45 global for the entire link; we assume that there is only a single
46 CONSTRUCTORS command in the linker script. */
47 boolean constructors_sorted;
48
49 /* The sets we have seen. */
50 struct set_info *sets;
51
52 /* Add an entry to a set. H is the entry in the linker hash table.
53 RELOC is the relocation to use for an entry in the set. SECTION
54 and VALUE are the value to add. This is called during the first
55 phase of the link, when we are still gathering symbols together.
56 We just record the information now. The ldctor_find_constructors
57 function will construct the sets. */
58
59 void
60 ldctor_add_set_entry (h, reloc, name, section, value)
61 struct bfd_link_hash_entry *h;
62 bfd_reloc_code_real_type reloc;
63 const char *name;
64 asection *section;
65 bfd_vma value;
66 {
67 struct set_info *p;
68 struct set_element *e;
69 struct set_element **epp;
70
71 for (p = sets; p != (struct set_info *) NULL; p = p->next)
72 if (p->h == h)
73 break;
74
75 if (p == (struct set_info *) NULL)
76 {
77 p = (struct set_info *) xmalloc (sizeof (struct set_info));
78 p->next = sets;
79 sets = p;
80 p->h = h;
81 p->reloc = reloc;
82 p->count = 0;
83 p->elements = NULL;
84 }
85 else
86 {
87 if (p->reloc != reloc)
88 {
89 einfo (_("%P%X: Different relocs used in set %s\n"), h->root.string);
90 return;
91 }
92
93 /* Don't permit a set to be constructed from different object
94 file formats. The same reloc may have different results. We
95 actually could sometimes handle this, but the case is
96 unlikely to ever arise. Sometimes constructor symbols are in
97 unusual sections, such as the absolute section--this appears
98 to be the case in Linux a.out--and in such cases we just
99 assume everything is OK. */
100 if (p->elements != NULL
101 && section->owner != NULL
102 && p->elements->section->owner != NULL
103 && strcmp (bfd_get_target (section->owner),
104 bfd_get_target (p->elements->section->owner)) != 0)
105 {
106 einfo (_("%P%X: Different object file formats composing set %s\n"),
107 h->root.string);
108 return;
109 }
110 }
111
112 e = (struct set_element *) xmalloc (sizeof (struct set_element));
113 e->next = NULL;
114 e->name = name;
115 e->section = section;
116 e->value = value;
117
118 for (epp = &p->elements; *epp != NULL; epp = &(*epp)->next)
119 ;
120 *epp = e;
121
122 ++p->count;
123 }
124
125 /* Get the priority of a g++ global constructor or destructor from the
126 symbol name. */
127
128 static int
129 ctor_prio (name)
130 const char *name;
131 {
132 /* The name will look something like _GLOBAL_$I$65535$test02__Fv.
133 There might be extra leading underscores, and the $ characters
134 might be something else. The I might be a D. */
135
136 while (*name == '_')
137 ++name;
138
139 if (strncmp (name, "GLOBAL_", sizeof "GLOBAL_" - 1) != 0)
140 return -1;
141
142 name += sizeof "GLOBAL_" - 1;
143
144 if (name[0] != name[2])
145 return -1;
146 if (name[1] != 'I' && name[1] != 'D')
147 return -1;
148 if (! isdigit ((unsigned char) name[3]))
149 return -1;
150
151 return atoi (name + 3);
152 }
153
154 /* This function is used to sort constructor elements by priority. It
155 is called via qsort. */
156
157 static int
158 ctor_cmp (p1, p2)
159 const PTR p1;
160 const PTR p2;
161 {
162 const struct set_element **pe1 = (const struct set_element **) p1;
163 const struct set_element **pe2 = (const struct set_element **) p2;
164 const char *n1;
165 const char *n2;
166 int prio1;
167 int prio2;
168 int ret;
169
170 n1 = (*pe1)->name;
171 if (n1 == NULL)
172 n1 = "";
173 n2 = (*pe2)->name;
174 if (n2 == NULL)
175 n2 = "";
176
177 /* We need to sort in reverse order by priority. When two
178 constructors have the same priority, we should maintain their
179 current relative position. */
180
181 prio1 = ctor_prio (n1);
182 prio2 = ctor_prio (n2);
183
184 /* We sort in reverse order because that is what g++ expects. */
185 if (prio1 < prio2)
186 return 1;
187 else if (prio1 > prio2)
188 return -1;
189
190 /* Force a stable sort. */
191
192 if (pe1 < pe2)
193 return -1;
194 else if (pe1 > pe2)
195 return 1;
196 else
197 return 0;
198 }
199
200 /* This function is called after the first phase of the link and
201 before the second phase. At this point all set information has
202 been gathered. We now put the statements to build the sets
203 themselves into constructor_list. */
204
205 void
206 ldctor_build_sets ()
207 {
208 static boolean called;
209 lang_statement_list_type *old;
210 boolean header_printed;
211 struct set_info *p;
212
213 /* The emulation code may call us directly, but we only want to do
214 this once. */
215 if (called)
216 return;
217 called = true;
218
219 if (constructors_sorted)
220 {
221 for (p = sets; p != NULL; p = p->next)
222 {
223 int c, i;
224 struct set_element *e;
225 struct set_element **array;
226
227 if (p->elements == NULL)
228 continue;
229
230 c = 0;
231 for (e = p->elements; e != NULL; e = e->next)
232 ++c;
233
234 array = (struct set_element **) xmalloc (c * sizeof *array);
235
236 i = 0;
237 for (e = p->elements; e != NULL; e = e->next)
238 {
239 array[i] = e;
240 ++i;
241 }
242
243 qsort (array, c, sizeof *array, ctor_cmp);
244
245 e = array[0];
246 p->elements = e;
247 for (i = 0; i < c - 1; i++)
248 array[i]->next = array[i + 1];
249 array[i]->next = NULL;
250
251 free (array);
252 }
253 }
254
255 old = stat_ptr;
256 stat_ptr = &constructor_list;
257
258 lang_list_init (stat_ptr);
259
260 header_printed = false;
261 for (p = sets; p != (struct set_info *) NULL; p = p->next)
262 {
263 struct set_element *e;
264 reloc_howto_type *howto;
265 int reloc_size, size;
266
267 /* If the symbol is defined, we may have been invoked from
268 collect, and the sets may already have been built, so we do
269 not do anything. */
270 if (p->h->type == bfd_link_hash_defined
271 || p->h->type == bfd_link_hash_defweak)
272 continue;
273
274 /* For each set we build:
275 set:
276 .long number_of_elements
277 .long element0
278 ...
279 .long elementN
280 .long 0
281 except that we use the right size instead of .long. When
282 generating relocateable output, we generate relocs instead of
283 addresses. */
284 howto = bfd_reloc_type_lookup (output_bfd, p->reloc);
285 if (howto == (reloc_howto_type *) NULL)
286 {
287 if (link_info.relocateable)
288 {
289 einfo (_("%P%X: %s does not support reloc %s for set %s\n"),
290 bfd_get_target (output_bfd),
291 bfd_get_reloc_code_name (p->reloc),
292 p->h->root.string);
293 continue;
294 }
295
296 /* If this is not a relocateable link, all we need is the
297 size, which we can get from the input BFD. */
298 howto = bfd_reloc_type_lookup (p->elements->section->owner,
299 p->reloc);
300 if (howto == NULL)
301 {
302 einfo (_("%P%X: %s does not support reloc %s for set %s\n"),
303 bfd_get_target (p->elements->section->owner),
304 bfd_get_reloc_code_name (p->reloc),
305 p->h->root.string);
306 continue;
307 }
308 }
309
310 reloc_size = bfd_get_reloc_size (howto);
311 switch (reloc_size)
312 {
313 case 1: size = BYTE; break;
314 case 2: size = SHORT; break;
315 case 4: size = LONG; break;
316 case 8:
317 if (howto->complain_on_overflow == complain_overflow_signed)
318 size = SQUAD;
319 else
320 size = QUAD;
321 break;
322 default:
323 einfo (_("%P%X: Unsupported size %d for set %s\n"),
324 bfd_get_reloc_size (howto), p->h->root.string);
325 size = LONG;
326 break;
327 }
328
329 lang_add_assignment (exp_assop ('=', ".",
330 exp_unop (ALIGN_K,
331 exp_intop (reloc_size))));
332 lang_add_assignment (exp_assop ('=', p->h->root.string,
333 exp_nameop (NAME, ".")));
334 lang_add_data (size, exp_intop ((bfd_vma) p->count));
335
336 for (e = p->elements; e != (struct set_element *) NULL; e = e->next)
337 {
338 if (config.map_file != NULL)
339 {
340 int len;
341
342 if (! header_printed)
343 {
344 minfo (_("\nSet Symbol\n\n"));
345 header_printed = true;
346 }
347
348 minfo ("%s", p->h->root.string);
349 len = strlen (p->h->root.string);
350
351 if (len >= 19)
352 {
353 print_nl ();
354 len = 0;
355 }
356 while (len < 20)
357 {
358 print_space ();
359 ++len;
360 }
361
362 if (e->name != NULL)
363 minfo ("%T\n", e->name);
364 else
365 minfo ("%G\n", e->section->owner, e->section, e->value);
366 }
367
368 if (link_info.relocateable)
369 lang_add_reloc (p->reloc, howto, e->section, e->name,
370 exp_intop (e->value));
371 else
372 lang_add_data (size, exp_relop (e->section, e->value));
373 }
374
375 lang_add_data (size, exp_intop (0));
376 }
377
378 stat_ptr = old;
379 }