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
;
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. */
181 prio1
= ctor_prio (n1
);
182 prio2
= ctor_prio (n2
);
184 /* We sort in reverse order because that is what g++ expects. */
187 else if (prio1
> prio2
)
190 /* Force a stable sort. */
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. */
208 static boolean called
;
209 lang_statement_list_type
*old
;
210 boolean header_printed
;
213 /* The emulation code may call us directly, but we only want to do
219 if (constructors_sorted
)
221 for (p
= sets
; p
!= NULL
; p
= p
->next
)
224 struct set_element
*e
;
225 struct set_element
**array
;
227 if (p
->elements
== NULL
)
231 for (e
= p
->elements
; e
!= NULL
; e
= e
->next
)
234 array
= (struct set_element
**) xmalloc (c
* sizeof *array
);
237 for (e
= p
->elements
; e
!= NULL
; e
= e
->next
)
243 qsort (array
, c
, sizeof *array
, ctor_cmp
);
247 for (i
= 0; i
< c
- 1; i
++)
248 array
[i
]->next
= array
[i
+ 1];
249 array
[i
]->next
= NULL
;
256 stat_ptr
= &constructor_list
;
258 lang_list_init (stat_ptr
);
260 header_printed
= false;
261 for (p
= sets
; p
!= (struct set_info
*) NULL
; p
= p
->next
)
263 struct set_element
*e
;
264 reloc_howto_type
*howto
;
265 int reloc_size
, size
;
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
270 if (p
->h
->type
== bfd_link_hash_defined
271 || p
->h
->type
== bfd_link_hash_defweak
)
274 /* For each set we build:
276 .long number_of_elements
281 except that we use the right size instead of .long. When
282 generating relocateable output, we generate relocs instead of
284 howto
= bfd_reloc_type_lookup (output_bfd
, p
->reloc
);
285 if (howto
== (reloc_howto_type
*) NULL
)
287 if (link_info
.relocateable
)
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
),
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
,
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 if (link_info
.relocateable
)
369 lang_add_reloc (p
->reloc
, howto
, e
->section
, e
->name
,
370 exp_intop (e
->value
));
372 lang_add_data (size
, exp_relop (e
->section
, e
->value
));
375 lang_add_data (size
, exp_intop (0));