1 /* ldctor.c -- constructor support routines
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011
4 Free Software Foundation, Inc.
5 By Steve Chamberlain <sac@cygnus.com>
7 This file is part of the GNU Binutils.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
27 #include "safe-ctype.h"
37 /* The list of statements needed to handle constructors. These are
38 invoked by the command CONSTRUCTORS in the linker script. */
39 lang_statement_list_type constructor_list
;
41 /* Whether the constructors should be sorted. Note that this is
42 global for the entire link; we assume that there is only a single
43 CONSTRUCTORS command in the linker script. */
44 bfd_boolean constructors_sorted
;
46 /* The sets we have seen. */
47 struct set_info
*sets
;
49 /* Add an entry to a set. H is the entry in the linker hash table.
50 RELOC is the relocation to use for an entry in the set. SECTION
51 and VALUE are the value to add. This is called during the first
52 phase of the link, when we are still gathering symbols together.
53 We just record the information now. The ldctor_build_sets
54 function will construct the sets. */
57 ldctor_add_set_entry (struct bfd_link_hash_entry
*h
,
58 bfd_reloc_code_real_type reloc
,
64 struct set_element
*e
;
65 struct set_element
**epp
;
67 for (p
= sets
; p
!= NULL
; p
= p
->next
)
73 p
= (struct set_info
*) xmalloc (sizeof (struct set_info
));
83 if (p
->reloc
!= reloc
)
85 einfo (_("%P%X: Different relocs used in set %s\n"),
90 /* Don't permit a set to be constructed from different object
91 file formats. The same reloc may have different results. We
92 actually could sometimes handle this, but the case is
93 unlikely to ever arise. Sometimes constructor symbols are in
94 unusual sections, such as the absolute section--this appears
95 to be the case in Linux a.out--and in such cases we just
96 assume everything is OK. */
97 if (p
->elements
!= NULL
98 && section
->owner
!= NULL
99 && p
->elements
->section
->owner
!= NULL
100 && strcmp (bfd_get_target (section
->owner
),
101 bfd_get_target (p
->elements
->section
->owner
)) != 0)
103 einfo (_("%P%X: Different object file formats composing set %s\n"),
109 e
= (struct set_element
*) xmalloc (sizeof (struct set_element
));
112 e
->section
= section
;
115 for (epp
= &p
->elements
; *epp
!= NULL
; epp
= &(*epp
)->next
)
122 /* Get the priority of a g++ global constructor or destructor from the
126 ctor_prio (const char *name
)
128 /* The name will look something like _GLOBAL_$I$65535$test02__Fv.
129 There might be extra leading underscores, and the $ characters
130 might be something else. The I might be a D. */
135 if (! CONST_STRNEQ (name
, "GLOBAL_"))
138 name
+= sizeof "GLOBAL_" - 1;
140 if (name
[0] != name
[2])
142 if (name
[1] != 'I' && name
[1] != 'D')
144 if (! ISDIGIT (name
[3]))
147 return atoi (name
+ 3);
150 /* This function is used to sort constructor elements by priority. It
151 is called via qsort. */
154 ctor_cmp (const void *p1
, const void *p2
)
156 const struct set_element
* const *pe1
=
157 (const struct set_element
* const *) p1
;
158 const struct set_element
* const *pe2
=
159 (const struct set_element
* const *) p2
;
172 /* We need to sort in reverse order by priority. When two
173 constructors have the same priority, we should maintain their
174 current relative position. */
176 prio1
= ctor_prio (n1
);
177 prio2
= ctor_prio (n2
);
179 /* We sort in reverse order because that is what g++ expects. */
182 else if (prio1
> prio2
)
185 /* Force a stable sort. */
195 /* This function is called after the first phase of the link and
196 before the second phase. At this point all set information has
197 been gathered. We now put the statements to build the sets
198 themselves into constructor_list. */
201 ldctor_build_sets (void)
203 static bfd_boolean called
;
204 bfd_boolean header_printed
;
207 /* The emulation code may call us directly, but we only want to do
213 if (constructors_sorted
)
215 for (p
= sets
; p
!= NULL
; p
= p
->next
)
218 struct set_element
*e
;
219 struct set_element
**array
;
221 if (p
->elements
== NULL
)
225 for (e
= p
->elements
; e
!= NULL
; e
= e
->next
)
228 array
= (struct set_element
**) xmalloc (c
* sizeof *array
);
231 for (e
= p
->elements
; e
!= NULL
; e
= e
->next
)
237 qsort (array
, c
, sizeof *array
, ctor_cmp
);
241 for (i
= 0; i
< c
- 1; i
++)
242 array
[i
]->next
= array
[i
+ 1];
243 array
[i
]->next
= NULL
;
249 lang_list_init (&constructor_list
);
250 push_stat_ptr (&constructor_list
);
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 (link_info
.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 (link_info
.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_assign (".",
324 exp_intop (reloc_size
))));
325 lang_add_assignment (exp_assign (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));