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>
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
37 static int ctor_prio
PARAMS ((const char *));
38 static int ctor_cmp
PARAMS ((const PTR
, const PTR
));
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
;
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
;
49 /* The sets we have seen. */
50 struct set_info
*sets
;
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. */
60 ldctor_add_set_entry (h
, reloc
, name
, section
, value
)
61 struct bfd_link_hash_entry
*h
;
62 bfd_reloc_code_real_type reloc
;
68 struct set_element
*e
;
69 struct set_element
**epp
;
71 for (p
= sets
; p
!= (struct set_info
*) NULL
; p
= p
->next
)
75 if (p
== (struct set_info
*) NULL
)
77 p
= (struct set_info
*) xmalloc (sizeof (struct set_info
));
87 if (p
->reloc
!= reloc
)
89 einfo (_("%P%X: Different relocs used in set %s\n"), h
->root
.string
);
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"),
112 e
= (struct set_element
*) xmalloc (sizeof (struct set_element
));
115 e
->section
= section
;
118 for (epp
= &p
->elements
; *epp
!= NULL
; epp
= &(*epp
)->next
)
125 /* Get the priority of a g++ global constructor or destructor from the
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. */
139 if (strncmp (name
, "GLOBAL_", sizeof "GLOBAL_" - 1) != 0)
142 name
+= sizeof "GLOBAL_" - 1;
144 if (name
[0] != name
[2])
146 if (name
[1] != 'I' && name
[1] != 'D')
148 if (! isdigit ((unsigned char) name
[3]))
151 return atoi (name
+ 3);
154 /* This function is used to sort constructor elements by priority. It
155 is called via qsort. */
162 const struct set_element
**pe1
= (const struct set_element
**) p1
;
163 const struct set_element
**pe2
= (const struct set_element
**) p2
;
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. */
186 else if (prio1
> prio2
)
189 /* Force a stable sort. */
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. */
207 static boolean called
;
208 lang_statement_list_type
*old
;
209 boolean header_printed
;
212 /* The emulation code may call us directly, but we only want to do
218 if (constructors_sorted
)
220 for (p
= sets
; p
!= NULL
; p
= p
->next
)
223 struct set_element
*e
;
224 struct set_element
**array
;
226 if (p
->elements
== NULL
)
230 for (e
= p
->elements
; e
!= NULL
; e
= e
->next
)
233 array
= (struct set_element
**) xmalloc (c
* sizeof *array
);
236 for (e
= p
->elements
; e
!= NULL
; e
= e
->next
)
242 qsort (array
, c
, sizeof *array
, ctor_cmp
);
246 for (i
= 0; i
< c
- 1; i
++)
247 array
[i
]->next
= array
[i
+ 1];
248 array
[i
]->next
= NULL
;
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
269 if (p
->h
->type
== bfd_link_hash_defined
270 || p
->h
->type
== bfd_link_hash_defweak
)
273 /* For each set we build:
275 .long number_of_elements
280 except that we use the right size instead of .long. When
281 generating relocateable output, we generate relocs instead of
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
),
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
,
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
),
310 reloc_size
= bfd_get_reloc_size (howto
);
313 case 1: size
= BYTE
; break;
314 case 2: size
= SHORT
; break;
315 case 4: size
= LONG
; break;
317 if (howto
->complain_on_overflow
== complain_overflow_signed
)
323 einfo (_("%P%X: Unsupported size %d for set %s\n"),
324 bfd_get_reloc_size (howto
), p
->h
->root
.string
);
329 lang_add_assignment (exp_assop ('=', ".",
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
)
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
);
363 minfo ("%T\n", e
->name
);
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
));
376 lang_add_data (size
, exp_relop (e
->section
, e
->value
));
379 lang_add_data (size
, exp_intop (0));