Add EXTERN references to __ctbpm __gp and __ep.
[binutils.git] / ld / ldctor.c
blob9e79921e91790e1b1c64d05750f70370869c6d06
1 /* ldctor.c -- constructor support routines
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4 By Steve Chamberlain <sac@cygnus.com>
6 This file is part of GLD, the Gnu Linker.
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.
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.
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. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "bfdlink.h"
26 #include "safe-ctype.h"
28 #include "ld.h"
29 #include "ldexp.h"
30 #include "ldlang.h"
31 #include "ldmisc.h"
32 #include "ldgram.h"
33 #include "ldmain.h"
34 #include "ldctor.h"
36 static int ctor_prio PARAMS ((const char *));
37 static int ctor_cmp PARAMS ((const PTR, const PTR));
39 /* The list of statements needed to handle constructors. These are
40 invoked by the command CONSTRUCTORS in the linker script. */
41 lang_statement_list_type constructor_list;
43 /* Whether the constructors should be sorted. Note that this is
44 global for the entire link; we assume that there is only a single
45 CONSTRUCTORS command in the linker script. */
46 boolean constructors_sorted;
48 /* The sets we have seen. */
49 struct set_info *sets;
51 /* Add an entry to a set. H is the entry in the linker hash table.
52 RELOC is the relocation to use for an entry in the set. SECTION
53 and VALUE are the value to add. This is called during the first
54 phase of the link, when we are still gathering symbols together.
55 We just record the information now. The ldctor_find_constructors
56 function will construct the sets. */
58 void
59 ldctor_add_set_entry (h, reloc, name, section, value)
60 struct bfd_link_hash_entry *h;
61 bfd_reloc_code_real_type reloc;
62 const char *name;
63 asection *section;
64 bfd_vma value;
66 struct set_info *p;
67 struct set_element *e;
68 struct set_element **epp;
70 for (p = sets; p != (struct set_info *) NULL; p = p->next)
71 if (p->h == h)
72 break;
74 if (p == (struct set_info *) NULL)
76 p = (struct set_info *) xmalloc (sizeof (struct set_info));
77 p->next = sets;
78 sets = p;
79 p->h = h;
80 p->reloc = reloc;
81 p->count = 0;
82 p->elements = NULL;
84 else
86 if (p->reloc != reloc)
88 einfo (_("%P%X: Different relocs used in set %s\n"),
89 h->root.string);
90 return;
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)
106 einfo (_("%P%X: Different object file formats composing set %s\n"),
107 h->root.string);
108 return;
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;
118 for (epp = &p->elements; *epp != NULL; epp = &(*epp)->next)
120 *epp = e;
122 ++p->count;
125 /* Get the priority of a g++ global constructor or destructor from the
126 symbol name. */
128 static int
129 ctor_prio (name)
130 const char *name;
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. */
136 while (*name == '_')
137 ++name;
139 if (strncmp (name, "GLOBAL_", sizeof "GLOBAL_" - 1) != 0)
140 return -1;
142 name += sizeof "GLOBAL_" - 1;
144 if (name[0] != name[2])
145 return -1;
146 if (name[1] != 'I' && name[1] != 'D')
147 return -1;
148 if (! ISDIGIT (name[3]))
149 return -1;
151 return atoi (name + 3);
154 /* This function is used to sort constructor elements by priority. It
155 is called via qsort. */
157 static int
158 ctor_cmp (p1, p2)
159 const PTR p1;
160 const PTR p2;
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;
169 n1 = (*pe1)->name;
170 if (n1 == NULL)
171 n1 = "";
172 n2 = (*pe2)->name;
173 if (n2 == NULL)
174 n2 = "";
176 /* We need to sort in reverse order by priority. When two
177 constructors have the same priority, we should maintain their
178 current relative position. */
180 prio1 = ctor_prio (n1);
181 prio2 = ctor_prio (n2);
183 /* We sort in reverse order because that is what g++ expects. */
184 if (prio1 < prio2)
185 return 1;
186 else if (prio1 > prio2)
187 return -1;
189 /* Force a stable sort. */
191 if (pe1 < pe2)
192 return -1;
193 else if (pe1 > pe2)
194 return 1;
195 else
196 return 0;
199 /* This function is called after the first phase of the link and
200 before the second phase. At this point all set information has
201 been gathered. We now put the statements to build the sets
202 themselves into constructor_list. */
204 void
205 ldctor_build_sets ()
207 static boolean called;
208 lang_statement_list_type *old;
209 boolean header_printed;
210 struct set_info *p;
212 /* The emulation code may call us directly, but we only want to do
213 this once. */
214 if (called)
215 return;
216 called = true;
218 if (constructors_sorted)
220 for (p = sets; p != NULL; p = p->next)
222 int c, i;
223 struct set_element *e;
224 struct set_element **array;
226 if (p->elements == NULL)
227 continue;
229 c = 0;
230 for (e = p->elements; e != NULL; e = e->next)
231 ++c;
233 array = (struct set_element **) xmalloc (c * sizeof *array);
235 i = 0;
236 for (e = p->elements; e != NULL; e = e->next)
238 array[i] = e;
239 ++i;
242 qsort (array, c, sizeof *array, ctor_cmp);
244 e = array[0];
245 p->elements = e;
246 for (i = 0; i < c - 1; i++)
247 array[i]->next = array[i + 1];
248 array[i]->next = NULL;
250 free (array);
254 old = stat_ptr;
255 stat_ptr = &constructor_list;
257 lang_list_init (stat_ptr);
259 header_printed = false;
260 for (p = sets; p != (struct set_info *) NULL; p = p->next)
262 struct set_element *e;
263 reloc_howto_type *howto;
264 int reloc_size, size;
266 /* If the symbol is defined, we may have been invoked from
267 collect, and the sets may already have been built, so we do
268 not do anything. */
269 if (p->h->type == bfd_link_hash_defined
270 || p->h->type == bfd_link_hash_defweak)
271 continue;
273 /* For each set we build:
274 set:
275 .long number_of_elements
276 .long element0
278 .long elementN
279 .long 0
280 except that we use the right size instead of .long. When
281 generating relocateable output, we generate relocs instead of
282 addresses. */
283 howto = bfd_reloc_type_lookup (output_bfd, p->reloc);
284 if (howto == (reloc_howto_type *) NULL)
286 if (link_info.relocateable)
288 einfo (_("%P%X: %s does not support reloc %s for set %s\n"),
289 bfd_get_target (output_bfd),
290 bfd_get_reloc_code_name (p->reloc),
291 p->h->root.string);
292 continue;
295 /* If this is not a relocateable link, all we need is the
296 size, which we can get from the input BFD. */
297 if (p->elements->section->owner != NULL)
298 howto = bfd_reloc_type_lookup (p->elements->section->owner,
299 p->reloc);
300 if (howto == NULL)
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;
310 reloc_size = bfd_get_reloc_size (howto);
311 switch (reloc_size)
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;
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));
336 for (e = p->elements; e != (struct set_element *) NULL; e = e->next)
338 if (config.map_file != NULL)
340 int len;
342 if (! header_printed)
344 minfo (_("\nSet Symbol\n\n"));
345 header_printed = true;
348 minfo ("%s", p->h->root.string);
349 len = strlen (p->h->root.string);
351 if (len >= 19)
353 print_nl ();
354 len = 0;
356 while (len < 20)
358 print_space ();
359 ++len;
362 if (e->name != NULL)
363 minfo ("%T\n", e->name);
364 else
365 minfo ("%G\n", e->section->owner, e->section, e->value);
368 /* Need SEC_KEEP for --gc-sections. */
369 if (! bfd_is_abs_section (e->section))
370 e->section->flags |= SEC_KEEP;
372 if (link_info.relocateable)
373 lang_add_reloc (p->reloc, howto, e->section, e->name,
374 exp_intop (e->value));
375 else
376 lang_add_data (size, exp_relop (e->section, e->value));
379 lang_add_data (size, exp_intop (0));
382 stat_ptr = old;