1 /* ldctor.c -- constructor support routines
2 Copyright (C) 1991-2018 Free Software Foundation, Inc.
3 By Steve Chamberlain <sac@cygnus.com>
5 This file is part of the GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
25 #include "safe-ctype.h"
35 /* The list of statements needed to handle constructors. These are
36 invoked by the command CONSTRUCTORS in the linker script. */
37 lang_statement_list_type constructor_list;
39 /* Whether the constructors should be sorted. Note that this is
40 global for the entire link; we assume that there is only a single
41 CONSTRUCTORS command in the linker script. */
42 bfd_boolean constructors_sorted;
44 /* The sets we have seen. */
45 struct set_info *sets;
47 /* Add an entry to a set. H is the entry in the linker hash table.
48 RELOC is the relocation to use for an entry in the set. SECTION
49 and VALUE are the value to add. This is called during the first
50 phase of the link, when we are still gathering symbols together.
51 We just record the information now. The ldctor_build_sets
52 function will construct the sets. */
55 ldctor_add_set_entry (struct bfd_link_hash_entry *h,
56 bfd_reloc_code_real_type reloc,
62 struct set_element *e;
63 struct set_element **epp;
65 for (p = sets; p != NULL; p = p->next)
71 p = (struct set_info *) xmalloc (sizeof (struct set_info));
81 if (p->reloc != reloc)
83 einfo (_("%X%P: different relocs used in set %s\n"),
88 /* Don't permit a set to be constructed from different object
89 file formats. The same reloc may have different results. We
90 actually could sometimes handle this, but the case is
91 unlikely to ever arise. Sometimes constructor symbols are in
92 unusual sections, such as the absolute section--this appears
93 to be the case in Linux a.out--and in such cases we just
94 assume everything is OK. */
95 if (p->elements != NULL
96 && section->owner != NULL
97 && p->elements->section->owner != NULL
98 && strcmp (bfd_get_target (section->owner),
99 bfd_get_target (p->elements->section->owner)) != 0)
101 einfo (_("%X%P: different object file formats composing set %s\n"),
107 e = (struct set_element *) xmalloc (sizeof (struct set_element));
110 e->section = section;
113 for (epp = &p->elements; *epp != NULL; epp = &(*epp)->next)
120 /* Get the priority of a g++ global constructor or destructor from the
124 ctor_prio (const char *name)
126 /* The name will look something like _GLOBAL_$I$65535$test02__Fv.
127 There might be extra leading underscores, and the $ characters
128 might be something else. The I might be a D. */
133 if (!CONST_STRNEQ (name, "GLOBAL_"))
136 name += sizeof "GLOBAL_" - 1;
138 if (name[0] != name[2])
140 if (name[1] != 'I' && name[1] != 'D')
142 if (!ISDIGIT (name[3]))
145 return atoi (name + 3);
148 /* This function is used to sort constructor elements by priority. It
149 is called via qsort. */
152 ctor_cmp (const void *p1, const void *p2)
154 const struct set_element *const *pe1
155 = (const struct set_element *const *) p1;
156 const struct set_element *const *pe2
157 = (const struct set_element *const *) p2;
170 /* We need to sort in reverse order by priority. When two
171 constructors have the same priority, we should maintain their
172 current relative position. */
174 prio1 = ctor_prio (n1);
175 prio2 = ctor_prio (n2);
177 /* We sort in reverse order because that is what g++ expects. */
180 else if (prio1 > prio2)
183 /* Force a stable sort. */
193 /* This function is called after the first phase of the link and
194 before the second phase. At this point all set information has
195 been gathered. We now put the statements to build the sets
196 themselves into constructor_list. */
199 ldctor_build_sets (void)
201 static bfd_boolean called;
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 = (struct set_element **) 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;
247 lang_list_init (&constructor_list);
248 push_stat_ptr (&constructor_list);
250 header_printed = FALSE;
251 for (p = sets; p != NULL; p = p->next)
253 struct set_element *e;
254 reloc_howto_type *howto;
255 int reloc_size, size;
257 /* If the symbol is defined, we may have been invoked from
258 collect, and the sets may already have been built, so we do
260 if (p->h->type == bfd_link_hash_defined
261 || p->h->type == bfd_link_hash_defweak)
264 /* For each set we build:
266 .long number_of_elements
271 except that we use the right size instead of .long. When
272 generating relocatable output, we generate relocs instead of
274 howto = bfd_reloc_type_lookup (link_info.output_bfd, p->reloc);
277 if (bfd_link_relocatable (&link_info))
279 einfo (_("%X%P: %s does not support reloc %s for set %s\n"),
280 bfd_get_target (link_info.output_bfd),
281 bfd_get_reloc_code_name (p->reloc),
286 /* If this is not a relocatable link, all we need is the
287 size, which we can get from the input BFD. */
288 if (p->elements->section->owner != NULL)
289 howto = bfd_reloc_type_lookup (p->elements->section->owner,
293 /* See PR 20911 for a reproducer. */
294 if (p->elements->section->owner == NULL)
295 einfo (_("%X%P: special section %s does not support reloc %s for set %s\n"),
296 bfd_get_section_name (link_info.output_bfd, p->elements->section),
297 bfd_get_reloc_code_name (p->reloc),
300 einfo (_("%X%P: %s does not support reloc %s for set %s\n"),
301 bfd_get_target (p->elements->section->owner),
302 bfd_get_reloc_code_name (p->reloc),
308 reloc_size = bfd_get_reloc_size (howto);
311 case 1: size = BYTE; break;
312 case 2: size = SHORT; break;
313 case 4: size = LONG; break;
315 if (howto->complain_on_overflow == complain_overflow_signed)
321 einfo (_("%X%P: unsupported size %d for set %s\n"),
322 bfd_get_reloc_size (howto), p->h->root.string);
327 lang_add_assignment (exp_assign (".",
329 exp_intop (reloc_size)),
331 lang_add_assignment (exp_assign (p->h->root.string,
332 exp_nameop (NAME, "."),
334 lang_add_data (size, exp_intop (p->count));
336 for (e = p->elements; e != NULL; e = e->next)
338 if (config.map_file != NULL)
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 ("%pT\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 (bfd_link_relocatable (&link_info))
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));