12a6b7db3Sskrll /* ldctor.c -- constructor support routines
2*e072ec67Schristos Copyright (C) 1991-2022 Free Software Foundation, Inc.
32a6b7db3Sskrll By Steve Chamberlain <sac@cygnus.com>
42a6b7db3Sskrll
52a6b7db3Sskrll This file is part of the GNU Binutils.
62a6b7db3Sskrll
72a6b7db3Sskrll This program is free software; you can redistribute it and/or modify
82a6b7db3Sskrll it under the terms of the GNU General Public License as published by
92a6b7db3Sskrll the Free Software Foundation; either version 3 of the License, or
102a6b7db3Sskrll (at your option) any later version.
112a6b7db3Sskrll
122a6b7db3Sskrll This program is distributed in the hope that it will be useful,
132a6b7db3Sskrll but WITHOUT ANY WARRANTY; without even the implied warranty of
142a6b7db3Sskrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
152a6b7db3Sskrll GNU General Public License for more details.
162a6b7db3Sskrll
172a6b7db3Sskrll You should have received a copy of the GNU General Public License
182a6b7db3Sskrll along with this program; if not, write to the Free Software
192a6b7db3Sskrll Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
202a6b7db3Sskrll MA 02110-1301, USA. */
212a6b7db3Sskrll
222a6b7db3Sskrll #include "sysdep.h"
232a6b7db3Sskrll #include "bfd.h"
242a6b7db3Sskrll #include "bfdlink.h"
252a6b7db3Sskrll #include "safe-ctype.h"
26c21fdd85Schristos #include "ctf-api.h"
272a6b7db3Sskrll
282a6b7db3Sskrll #include "ld.h"
292a6b7db3Sskrll #include "ldexp.h"
302a6b7db3Sskrll #include "ldlang.h"
312a6b7db3Sskrll #include "ldmisc.h"
322a6b7db3Sskrll #include <ldgram.h>
332a6b7db3Sskrll #include "ldmain.h"
342a6b7db3Sskrll #include "ldctor.h"
352a6b7db3Sskrll
362a6b7db3Sskrll /* The list of statements needed to handle constructors. These are
372a6b7db3Sskrll invoked by the command CONSTRUCTORS in the linker script. */
382a6b7db3Sskrll lang_statement_list_type constructor_list;
392a6b7db3Sskrll
402a6b7db3Sskrll /* Whether the constructors should be sorted. Note that this is
412a6b7db3Sskrll global for the entire link; we assume that there is only a single
422a6b7db3Sskrll CONSTRUCTORS command in the linker script. */
43*e072ec67Schristos bool constructors_sorted;
442a6b7db3Sskrll
452a6b7db3Sskrll /* The sets we have seen. */
462a6b7db3Sskrll struct set_info *sets;
472a6b7db3Sskrll
482a6b7db3Sskrll /* Add an entry to a set. H is the entry in the linker hash table.
492a6b7db3Sskrll RELOC is the relocation to use for an entry in the set. SECTION
502a6b7db3Sskrll and VALUE are the value to add. This is called during the first
512a6b7db3Sskrll phase of the link, when we are still gathering symbols together.
522a6b7db3Sskrll We just record the information now. The ldctor_build_sets
532a6b7db3Sskrll function will construct the sets. */
542a6b7db3Sskrll
552a6b7db3Sskrll void
ldctor_add_set_entry(struct bfd_link_hash_entry * h,bfd_reloc_code_real_type reloc,const char * name,asection * section,bfd_vma value)562a6b7db3Sskrll ldctor_add_set_entry (struct bfd_link_hash_entry *h,
572a6b7db3Sskrll bfd_reloc_code_real_type reloc,
582a6b7db3Sskrll const char *name,
592a6b7db3Sskrll asection *section,
602a6b7db3Sskrll bfd_vma value)
612a6b7db3Sskrll {
622a6b7db3Sskrll struct set_info *p;
632a6b7db3Sskrll struct set_element *e;
642a6b7db3Sskrll struct set_element **epp;
652a6b7db3Sskrll
662a6b7db3Sskrll for (p = sets; p != NULL; p = p->next)
672a6b7db3Sskrll if (p->h == h)
682a6b7db3Sskrll break;
692a6b7db3Sskrll
702a6b7db3Sskrll if (p == NULL)
712a6b7db3Sskrll {
728443c5fcSchristos p = (struct set_info *) xmalloc (sizeof (struct set_info));
732a6b7db3Sskrll p->next = sets;
742a6b7db3Sskrll sets = p;
752a6b7db3Sskrll p->h = h;
762a6b7db3Sskrll p->reloc = reloc;
772a6b7db3Sskrll p->count = 0;
782a6b7db3Sskrll p->elements = NULL;
792a6b7db3Sskrll }
802a6b7db3Sskrll else
812a6b7db3Sskrll {
822a6b7db3Sskrll if (p->reloc != reloc)
832a6b7db3Sskrll {
84916041edSchristos einfo (_("%X%P: different relocs used in set %s\n"),
852a6b7db3Sskrll h->root.string);
862a6b7db3Sskrll return;
872a6b7db3Sskrll }
882a6b7db3Sskrll
892a6b7db3Sskrll /* Don't permit a set to be constructed from different object
902a6b7db3Sskrll file formats. The same reloc may have different results. We
912a6b7db3Sskrll actually could sometimes handle this, but the case is
922a6b7db3Sskrll unlikely to ever arise. Sometimes constructor symbols are in
932a6b7db3Sskrll unusual sections, such as the absolute section--this appears
942a6b7db3Sskrll to be the case in Linux a.out--and in such cases we just
952a6b7db3Sskrll assume everything is OK. */
962a6b7db3Sskrll if (p->elements != NULL
972a6b7db3Sskrll && section->owner != NULL
982a6b7db3Sskrll && p->elements->section->owner != NULL
992a6b7db3Sskrll && strcmp (bfd_get_target (section->owner),
1002a6b7db3Sskrll bfd_get_target (p->elements->section->owner)) != 0)
1012a6b7db3Sskrll {
102916041edSchristos einfo (_("%X%P: different object file formats composing set %s\n"),
1032a6b7db3Sskrll h->root.string);
1042a6b7db3Sskrll return;
1052a6b7db3Sskrll }
1062a6b7db3Sskrll }
1072a6b7db3Sskrll
1088443c5fcSchristos e = (struct set_element *) xmalloc (sizeof (struct set_element));
109c21fdd85Schristos e->u.next = NULL;
1102a6b7db3Sskrll e->name = name;
1112a6b7db3Sskrll e->section = section;
1122a6b7db3Sskrll e->value = value;
1132a6b7db3Sskrll
114c21fdd85Schristos for (epp = &p->elements; *epp != NULL; epp = &(*epp)->u.next)
1152a6b7db3Sskrll ;
1162a6b7db3Sskrll *epp = e;
1172a6b7db3Sskrll
1182a6b7db3Sskrll ++p->count;
1192a6b7db3Sskrll }
1202a6b7db3Sskrll
1212a6b7db3Sskrll /* Get the priority of a g++ global constructor or destructor from the
1222a6b7db3Sskrll symbol name. */
1232a6b7db3Sskrll
1242a6b7db3Sskrll static int
ctor_prio(const char * name)1252a6b7db3Sskrll ctor_prio (const char *name)
1262a6b7db3Sskrll {
1272a6b7db3Sskrll /* The name will look something like _GLOBAL_$I$65535$test02__Fv.
1282a6b7db3Sskrll There might be extra leading underscores, and the $ characters
1292a6b7db3Sskrll might be something else. The I might be a D. */
1302a6b7db3Sskrll
1312a6b7db3Sskrll while (*name == '_')
1322a6b7db3Sskrll ++name;
1332a6b7db3Sskrll
134*e072ec67Schristos if (!startswith (name, "GLOBAL_"))
1352a6b7db3Sskrll return -1;
1362a6b7db3Sskrll
1372a6b7db3Sskrll name += sizeof "GLOBAL_" - 1;
1382a6b7db3Sskrll
1392a6b7db3Sskrll if (name[0] != name[2])
1402a6b7db3Sskrll return -1;
1412a6b7db3Sskrll if (name[1] != 'I' && name[1] != 'D')
1422a6b7db3Sskrll return -1;
1432a6b7db3Sskrll if (!ISDIGIT (name[3]))
1442a6b7db3Sskrll return -1;
1452a6b7db3Sskrll
1462a6b7db3Sskrll return atoi (name + 3);
1472a6b7db3Sskrll }
1482a6b7db3Sskrll
1492a6b7db3Sskrll /* This function is used to sort constructor elements by priority. It
1502a6b7db3Sskrll is called via qsort. */
1512a6b7db3Sskrll
1522a6b7db3Sskrll static int
ctor_cmp(const void * p1,const void * p2)1532a6b7db3Sskrll ctor_cmp (const void *p1, const void *p2)
1542a6b7db3Sskrll {
155c21fdd85Schristos const struct set_element *pe1 = *(const struct set_element **) p1;
156c21fdd85Schristos const struct set_element *pe2 = *(const struct set_element **) p2;
1572a6b7db3Sskrll const char *n1;
1582a6b7db3Sskrll const char *n2;
1592a6b7db3Sskrll int prio1;
1602a6b7db3Sskrll int prio2;
1612a6b7db3Sskrll
162c21fdd85Schristos n1 = pe1->name;
1632a6b7db3Sskrll if (n1 == NULL)
1642a6b7db3Sskrll n1 = "";
165c21fdd85Schristos n2 = pe2->name;
1662a6b7db3Sskrll if (n2 == NULL)
1672a6b7db3Sskrll n2 = "";
1682a6b7db3Sskrll
1692a6b7db3Sskrll /* We need to sort in reverse order by priority. When two
1702a6b7db3Sskrll constructors have the same priority, we should maintain their
1712a6b7db3Sskrll current relative position. */
1722a6b7db3Sskrll
1732a6b7db3Sskrll prio1 = ctor_prio (n1);
1742a6b7db3Sskrll prio2 = ctor_prio (n2);
1752a6b7db3Sskrll
1762a6b7db3Sskrll /* We sort in reverse order because that is what g++ expects. */
1772a6b7db3Sskrll if (prio1 < prio2)
1782a6b7db3Sskrll return 1;
179c21fdd85Schristos if (prio1 > prio2)
1802a6b7db3Sskrll return -1;
1812a6b7db3Sskrll
1822a6b7db3Sskrll /* Force a stable sort. */
183c21fdd85Schristos if (pe1->u.idx < pe2->u.idx)
1842a6b7db3Sskrll return -1;
185c21fdd85Schristos if (pe1->u.idx > pe2->u.idx)
1862a6b7db3Sskrll return 1;
1872a6b7db3Sskrll return 0;
1882a6b7db3Sskrll }
1892a6b7db3Sskrll
1902a6b7db3Sskrll /* This function is called after the first phase of the link and
1912a6b7db3Sskrll before the second phase. At this point all set information has
1922a6b7db3Sskrll been gathered. We now put the statements to build the sets
1932a6b7db3Sskrll themselves into constructor_list. */
1942a6b7db3Sskrll
1952a6b7db3Sskrll void
ldctor_build_sets(void)1962a6b7db3Sskrll ldctor_build_sets (void)
1972a6b7db3Sskrll {
198*e072ec67Schristos static bool called;
199*e072ec67Schristos bool header_printed;
2002a6b7db3Sskrll struct set_info *p;
2012a6b7db3Sskrll
2022a6b7db3Sskrll /* The emulation code may call us directly, but we only want to do
2032a6b7db3Sskrll this once. */
2042a6b7db3Sskrll if (called)
2052a6b7db3Sskrll return;
206*e072ec67Schristos called = true;
2072a6b7db3Sskrll
2082a6b7db3Sskrll if (constructors_sorted)
2092a6b7db3Sskrll {
2102a6b7db3Sskrll for (p = sets; p != NULL; p = p->next)
2112a6b7db3Sskrll {
2122a6b7db3Sskrll int c, i;
213c21fdd85Schristos struct set_element *e, *enext;
2142a6b7db3Sskrll struct set_element **array;
2152a6b7db3Sskrll
2162a6b7db3Sskrll if (p->elements == NULL)
2172a6b7db3Sskrll continue;
2182a6b7db3Sskrll
2192a6b7db3Sskrll c = 0;
220c21fdd85Schristos for (e = p->elements; e != NULL; e = e->u.next)
2212a6b7db3Sskrll ++c;
2222a6b7db3Sskrll
2238443c5fcSchristos array = (struct set_element **) xmalloc (c * sizeof *array);
2242a6b7db3Sskrll
2252a6b7db3Sskrll i = 0;
226c21fdd85Schristos for (e = p->elements; e != NULL; e = enext)
2272a6b7db3Sskrll {
2282a6b7db3Sskrll array[i] = e;
229c21fdd85Schristos enext = e->u.next;
230c21fdd85Schristos e->u.idx = i;
2312a6b7db3Sskrll ++i;
2322a6b7db3Sskrll }
2332a6b7db3Sskrll
2342a6b7db3Sskrll qsort (array, c, sizeof *array, ctor_cmp);
2352a6b7db3Sskrll
2362a6b7db3Sskrll e = array[0];
2372a6b7db3Sskrll p->elements = e;
2382a6b7db3Sskrll for (i = 0; i < c - 1; i++)
239c21fdd85Schristos array[i]->u.next = array[i + 1];
240c21fdd85Schristos array[i]->u.next = NULL;
2412a6b7db3Sskrll
2422a6b7db3Sskrll free (array);
2432a6b7db3Sskrll }
2442a6b7db3Sskrll }
2452a6b7db3Sskrll
246af515df4Sskrll lang_list_init (&constructor_list);
247af515df4Sskrll push_stat_ptr (&constructor_list);
2482a6b7db3Sskrll
249*e072ec67Schristos header_printed = false;
2502a6b7db3Sskrll for (p = sets; p != NULL; p = p->next)
2512a6b7db3Sskrll {
2522a6b7db3Sskrll struct set_element *e;
2532a6b7db3Sskrll reloc_howto_type *howto;
2542a6b7db3Sskrll int reloc_size, size;
2552a6b7db3Sskrll
2562a6b7db3Sskrll /* If the symbol is defined, we may have been invoked from
2572a6b7db3Sskrll collect, and the sets may already have been built, so we do
2582a6b7db3Sskrll not do anything. */
2592a6b7db3Sskrll if (p->h->type == bfd_link_hash_defined
2602a6b7db3Sskrll || p->h->type == bfd_link_hash_defweak)
2612a6b7db3Sskrll continue;
2622a6b7db3Sskrll
2632a6b7db3Sskrll /* For each set we build:
2642a6b7db3Sskrll set:
2652a6b7db3Sskrll .long number_of_elements
2662a6b7db3Sskrll .long element0
2672a6b7db3Sskrll ...
2682a6b7db3Sskrll .long elementN
2692a6b7db3Sskrll .long 0
2702a6b7db3Sskrll except that we use the right size instead of .long. When
2712a6b7db3Sskrll generating relocatable output, we generate relocs instead of
2722a6b7db3Sskrll addresses. */
2732a6b7db3Sskrll howto = bfd_reloc_type_lookup (link_info.output_bfd, p->reloc);
2742a6b7db3Sskrll if (howto == NULL)
2752a6b7db3Sskrll {
276438aacb6Schristos if (bfd_link_relocatable (&link_info))
2772a6b7db3Sskrll {
278916041edSchristos einfo (_("%X%P: %s does not support reloc %s for set %s\n"),
2792a6b7db3Sskrll bfd_get_target (link_info.output_bfd),
2802a6b7db3Sskrll bfd_get_reloc_code_name (p->reloc),
2812a6b7db3Sskrll p->h->root.string);
2822a6b7db3Sskrll continue;
2832a6b7db3Sskrll }
2842a6b7db3Sskrll
2852a6b7db3Sskrll /* If this is not a relocatable link, all we need is the
2862a6b7db3Sskrll size, which we can get from the input BFD. */
2872a6b7db3Sskrll if (p->elements->section->owner != NULL)
2882a6b7db3Sskrll howto = bfd_reloc_type_lookup (p->elements->section->owner,
2892a6b7db3Sskrll p->reloc);
2902a6b7db3Sskrll if (howto == NULL)
2912a6b7db3Sskrll {
292aa4b58b1Schristos /* See PR 20911 for a reproducer. */
293aa4b58b1Schristos if (p->elements->section->owner == NULL)
294916041edSchristos einfo (_("%X%P: special section %s does not support reloc %s for set %s\n"),
295c21fdd85Schristos bfd_section_name (p->elements->section),
296aa4b58b1Schristos bfd_get_reloc_code_name (p->reloc),
297aa4b58b1Schristos p->h->root.string);
298aa4b58b1Schristos else
299916041edSchristos einfo (_("%X%P: %s does not support reloc %s for set %s\n"),
3002a6b7db3Sskrll bfd_get_target (p->elements->section->owner),
3012a6b7db3Sskrll bfd_get_reloc_code_name (p->reloc),
3022a6b7db3Sskrll p->h->root.string);
3032a6b7db3Sskrll continue;
3042a6b7db3Sskrll }
3052a6b7db3Sskrll }
3062a6b7db3Sskrll
3072a6b7db3Sskrll reloc_size = bfd_get_reloc_size (howto);
3082a6b7db3Sskrll switch (reloc_size)
3092a6b7db3Sskrll {
3102a6b7db3Sskrll case 1: size = BYTE; break;
3112a6b7db3Sskrll case 2: size = SHORT; break;
3122a6b7db3Sskrll case 4: size = LONG; break;
3132a6b7db3Sskrll case 8:
3142a6b7db3Sskrll if (howto->complain_on_overflow == complain_overflow_signed)
3152a6b7db3Sskrll size = SQUAD;
3162a6b7db3Sskrll else
3172a6b7db3Sskrll size = QUAD;
3182a6b7db3Sskrll break;
3192a6b7db3Sskrll default:
320916041edSchristos einfo (_("%X%P: unsupported size %d for set %s\n"),
3212a6b7db3Sskrll bfd_get_reloc_size (howto), p->h->root.string);
3222a6b7db3Sskrll size = LONG;
3232a6b7db3Sskrll break;
3242a6b7db3Sskrll }
3252a6b7db3Sskrll
3268443c5fcSchristos lang_add_assignment (exp_assign (".",
3272a6b7db3Sskrll exp_unop (ALIGN_K,
3284c43201bSchristos exp_intop (reloc_size)),
329*e072ec67Schristos false));
3308443c5fcSchristos lang_add_assignment (exp_assign (p->h->root.string,
3314c43201bSchristos exp_nameop (NAME, "."),
332*e072ec67Schristos false));
3332a6b7db3Sskrll lang_add_data (size, exp_intop (p->count));
3342a6b7db3Sskrll
335c21fdd85Schristos for (e = p->elements; e != NULL; e = e->u.next)
3362a6b7db3Sskrll {
3372a6b7db3Sskrll if (config.map_file != NULL)
3382a6b7db3Sskrll {
3392a6b7db3Sskrll int len;
3402a6b7db3Sskrll
3412a6b7db3Sskrll if (!header_printed)
3422a6b7db3Sskrll {
3432a6b7db3Sskrll minfo (_("\nSet Symbol\n\n"));
344*e072ec67Schristos header_printed = true;
3452a6b7db3Sskrll }
3462a6b7db3Sskrll
3472a6b7db3Sskrll minfo ("%s", p->h->root.string);
3482a6b7db3Sskrll len = strlen (p->h->root.string);
3492a6b7db3Sskrll
3502a6b7db3Sskrll if (len >= 19)
3512a6b7db3Sskrll {
3522a6b7db3Sskrll print_nl ();
3532a6b7db3Sskrll len = 0;
3542a6b7db3Sskrll }
3552a6b7db3Sskrll while (len < 20)
3562a6b7db3Sskrll {
3572a6b7db3Sskrll print_space ();
3582a6b7db3Sskrll ++len;
3592a6b7db3Sskrll }
3602a6b7db3Sskrll
3612a6b7db3Sskrll if (e->name != NULL)
362916041edSchristos minfo ("%pT\n", e->name);
3632a6b7db3Sskrll else
3642a6b7db3Sskrll minfo ("%G\n", e->section->owner, e->section, e->value);
3652a6b7db3Sskrll }
3662a6b7db3Sskrll
3672a6b7db3Sskrll /* Need SEC_KEEP for --gc-sections. */
3682a6b7db3Sskrll if (!bfd_is_abs_section (e->section))
3692a6b7db3Sskrll e->section->flags |= SEC_KEEP;
3702a6b7db3Sskrll
371438aacb6Schristos if (bfd_link_relocatable (&link_info))
3722a6b7db3Sskrll lang_add_reloc (p->reloc, howto, e->section, e->name,
3732a6b7db3Sskrll exp_intop (e->value));
3742a6b7db3Sskrll else
3752a6b7db3Sskrll lang_add_data (size, exp_relop (e->section, e->value));
3762a6b7db3Sskrll }
3772a6b7db3Sskrll
3782a6b7db3Sskrll lang_add_data (size, exp_intop (0));
3792a6b7db3Sskrll }
3802a6b7db3Sskrll
381af515df4Sskrll pop_stat_ptr ();
3822a6b7db3Sskrll }
383