1 /* ldctor.c -- constructor support routines
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003 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)
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
26 #include "safe-ctype.h"
36 /* The list of statements needed to handle constructors. These are
37 invoked by the command CONSTRUCTORS in the linker script. */
38 lang_statement_list_type constructor_list
;
40 /* Whether the constructors should be sorted. Note that this is
41 global for the entire link; we assume that there is only a single
42 CONSTRUCTORS command in the linker script. */
43 bfd_boolean constructors_sorted
;
45 /* The sets we have seen. */
46 struct set_info
*sets
;
48 /* Add an entry to a set. H is the entry in the linker hash table.
49 RELOC is the relocation to use for an entry in the set. SECTION
50 and VALUE are the value to add. This is called during the first
51 phase of the link, when we are still gathering symbols together.
52 We just record the information now. The ldctor_find_constructors
53 function will construct the sets. */
56 ldctor_add_set_entry (struct bfd_link_hash_entry
*h
,
57 bfd_reloc_code_real_type reloc
,
63 struct set_element
*e
;
64 struct set_element
**epp
;
66 for (p
= sets
; p
!= NULL
; p
= p
->next
)
72 p
= xmalloc (sizeof (struct set_info
));
82 if (p
->reloc
!= reloc
)
84 einfo (_("%P%X: Different relocs used in set %s\n"),
89 /* Don't permit a set to be constructed from different object
90 file formats. The same reloc may have different results. We
91 actually could sometimes handle this, but the case is
92 unlikely to ever arise. Sometimes constructor symbols are in
93 unusual sections, such as the absolute section--this appears
94 to be the case in Linux a.out--and in such cases we just
95 assume everything is OK. */
96 if (p
->elements
!= NULL
97 && section
->owner
!= NULL
98 && p
->elements
->section
->owner
!= NULL
99 && strcmp (bfd_get_target (section
->owner
),
100 bfd_get_target (p
->elements
->section
->owner
)) != 0)
102 einfo (_("%P%X: Different object file formats composing set %s\n"),
108 e
= xmalloc (sizeof (struct set_element
));
111 e
->section
= section
;
114 for (epp
= &p
->elements
; *epp
!= NULL
; epp
= &(*epp
)->next
)
121 /* Get the priority of a g++ global constructor or destructor from the
125 ctor_prio (const char *name
)
127 /* The name will look something like _GLOBAL_$I$65535$test02__Fv.
128 There might be extra leading underscores, and the $ characters
129 might be something else. The I might be a D. */
134 if (strncmp (name
, "GLOBAL_", sizeof "GLOBAL_" - 1) != 0)
137 name
+= sizeof "GLOBAL_" - 1;
139 if (name
[0] != name
[2])
141 if (name
[1] != 'I' && name
[1] != 'D')
143 if (! ISDIGIT (name
[3]))
146 return atoi (name
+ 3);
149 /* This function is used to sort constructor elements by priority. It
150 is called via qsort. */
153 ctor_cmp (const void *p1
, const void *p2
)
155 const struct set_element
* const *pe1
= p1
;
156 const struct set_element
* const *pe2
= p2
;
169 /* We need to sort in reverse order by priority. When two
170 constructors have the same priority, we should maintain their
171 current relative position. */
173 prio1
= ctor_prio (n1
);
174 prio2
= ctor_prio (n2
);
176 /* We sort in reverse order because that is what g++ expects. */
179 else if (prio1
> prio2
)
182 /* Force a stable sort. */
192 /* This function is called after the first phase of the link and
193 before the second phase. At this point all set information has
194 been gathered. We now put the statements to build the sets
195 themselves into constructor_list. */
198 ldctor_build_sets (void)
200 static bfd_boolean called
;
201 lang_statement_list_type
*old
;
202 bfd_boolean header_printed
;
205 /* The emulation code may call us directly, but we only want to do
211 if (constructors_sorted
)
213 for (p
= sets
; p
!= NULL
; p
= p
->next
)
216 struct set_element
*e
;
217 struct set_element
**array
;
219 if (p
->elements
== NULL
)
223 for (e
= p
->elements
; e
!= NULL
; e
= e
->next
)
226 array
= xmalloc (c
* sizeof *array
);
229 for (e
= p
->elements
; e
!= NULL
; e
= e
->next
)
235 qsort (array
, c
, sizeof *array
, ctor_cmp
);
239 for (i
= 0; i
< c
- 1; i
++)
240 array
[i
]->next
= array
[i
+ 1];
241 array
[i
]->next
= NULL
;
248 stat_ptr
= &constructor_list
;
250 lang_list_init (stat_ptr
);
252 header_printed
= FALSE
;
253 for (p
= sets
; p
!= NULL
; p
= p
->next
)
255 struct set_element
*e
;
256 reloc_howto_type
*howto
;
257 int reloc_size
, size
;
259 /* If the symbol is defined, we may have been invoked from
260 collect, and the sets may already have been built, so we do
262 if (p
->h
->type
== bfd_link_hash_defined
263 || p
->h
->type
== bfd_link_hash_defweak
)
266 /* For each set we build:
268 .long number_of_elements
273 except that we use the right size instead of .long. When
274 generating relocatable output, we generate relocs instead of
276 howto
= bfd_reloc_type_lookup (output_bfd
, p
->reloc
);
279 if (link_info
.relocatable
)
281 einfo (_("%P%X: %s does not support reloc %s for set %s\n"),
282 bfd_get_target (output_bfd
),
283 bfd_get_reloc_code_name (p
->reloc
),
288 /* If this is not a relocatable link, all we need is the
289 size, which we can get from the input BFD. */
290 if (p
->elements
->section
->owner
!= NULL
)
291 howto
= bfd_reloc_type_lookup (p
->elements
->section
->owner
,
295 einfo (_("%P%X: %s does not support reloc %s for set %s\n"),
296 bfd_get_target (p
->elements
->section
->owner
),
297 bfd_get_reloc_code_name (p
->reloc
),
303 reloc_size
= bfd_get_reloc_size (howto
);
306 case 1: size
= BYTE
; break;
307 case 2: size
= SHORT
; break;
308 case 4: size
= LONG
; break;
310 if (howto
->complain_on_overflow
== complain_overflow_signed
)
316 einfo (_("%P%X: Unsupported size %d for set %s\n"),
317 bfd_get_reloc_size (howto
), p
->h
->root
.string
);
322 lang_add_assignment (exp_assop ('=', ".",
324 exp_intop (reloc_size
))));
325 lang_add_assignment (exp_assop ('=', p
->h
->root
.string
,
326 exp_nameop (NAME
, ".")));
327 lang_add_data (size
, exp_intop (p
->count
));
329 for (e
= p
->elements
; e
!= NULL
; e
= e
->next
)
331 if (config
.map_file
!= NULL
)
335 if (! header_printed
)
337 minfo (_("\nSet Symbol\n\n"));
338 header_printed
= TRUE
;
341 minfo ("%s", p
->h
->root
.string
);
342 len
= strlen (p
->h
->root
.string
);
356 minfo ("%T\n", e
->name
);
358 minfo ("%G\n", e
->section
->owner
, e
->section
, e
->value
);
361 /* Need SEC_KEEP for --gc-sections. */
362 if (! bfd_is_abs_section (e
->section
))
363 e
->section
->flags
|= SEC_KEEP
;
365 if (link_info
.relocatable
)
366 lang_add_reloc (p
->reloc
, howto
, e
->section
, e
->name
,
367 exp_intop (e
->value
));
369 lang_add_data (size
, exp_relop (e
->section
, e
->value
));
372 lang_add_data (size
, exp_intop (0));