12a6b7db3Sskrll /* ldctor.c -- constructor support routines
2*dd7241dfSchristos Copyright (C) 1991-2024 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"
23*dd7241dfSchristos #include "libiberty.h"
242a6b7db3Sskrll #include "bfd.h"
252a6b7db3Sskrll #include "bfdlink.h"
262a6b7db3Sskrll #include "safe-ctype.h"
27106c59e5Schristos #include "ctf-api.h"
282a6b7db3Sskrll
292a6b7db3Sskrll #include "ld.h"
302a6b7db3Sskrll #include "ldexp.h"
312a6b7db3Sskrll #include "ldlang.h"
322a6b7db3Sskrll #include "ldmisc.h"
332a6b7db3Sskrll #include <ldgram.h>
342a6b7db3Sskrll #include "ldmain.h"
352a6b7db3Sskrll #include "ldctor.h"
362a6b7db3Sskrll
372a6b7db3Sskrll /* The list of statements needed to handle constructors. These are
382a6b7db3Sskrll invoked by the command CONSTRUCTORS in the linker script. */
392a6b7db3Sskrll lang_statement_list_type constructor_list;
402a6b7db3Sskrll
412a6b7db3Sskrll /* Whether the constructors should be sorted. Note that this is
422a6b7db3Sskrll global for the entire link; we assume that there is only a single
432a6b7db3Sskrll CONSTRUCTORS command in the linker script. */
4403f5171aSchristos bool constructors_sorted;
452a6b7db3Sskrll
462a6b7db3Sskrll /* The sets we have seen. */
472a6b7db3Sskrll struct set_info *sets;
482a6b7db3Sskrll
492a6b7db3Sskrll /* Add an entry to a set. H is the entry in the linker hash table.
502a6b7db3Sskrll RELOC is the relocation to use for an entry in the set. SECTION
512a6b7db3Sskrll and VALUE are the value to add. This is called during the first
522a6b7db3Sskrll phase of the link, when we are still gathering symbols together.
532a6b7db3Sskrll We just record the information now. The ldctor_build_sets
542a6b7db3Sskrll function will construct the sets. */
552a6b7db3Sskrll
562a6b7db3Sskrll 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)572a6b7db3Sskrll ldctor_add_set_entry (struct bfd_link_hash_entry *h,
582a6b7db3Sskrll bfd_reloc_code_real_type reloc,
592a6b7db3Sskrll const char *name,
602a6b7db3Sskrll asection *section,
612a6b7db3Sskrll bfd_vma value)
622a6b7db3Sskrll {
632a6b7db3Sskrll struct set_info *p;
642a6b7db3Sskrll struct set_element *e;
652a6b7db3Sskrll struct set_element **epp;
662a6b7db3Sskrll
672a6b7db3Sskrll for (p = sets; p != NULL; p = p->next)
682a6b7db3Sskrll if (p->h == h)
692a6b7db3Sskrll break;
702a6b7db3Sskrll
712a6b7db3Sskrll if (p == NULL)
722a6b7db3Sskrll {
7332998b1cSchristos p = (struct set_info *) xmalloc (sizeof (struct set_info));
742a6b7db3Sskrll p->next = sets;
752a6b7db3Sskrll sets = p;
762a6b7db3Sskrll p->h = h;
772a6b7db3Sskrll p->reloc = reloc;
782a6b7db3Sskrll p->count = 0;
792a6b7db3Sskrll p->elements = NULL;
802a6b7db3Sskrll }
812a6b7db3Sskrll else
822a6b7db3Sskrll {
832a6b7db3Sskrll if (p->reloc != reloc)
842a6b7db3Sskrll {
857c6f6726Schristos einfo (_("%X%P: different relocs used in set %s\n"),
862a6b7db3Sskrll h->root.string);
872a6b7db3Sskrll return;
882a6b7db3Sskrll }
892a6b7db3Sskrll
902a6b7db3Sskrll /* Don't permit a set to be constructed from different object
912a6b7db3Sskrll file formats. The same reloc may have different results. We
922a6b7db3Sskrll actually could sometimes handle this, but the case is
932a6b7db3Sskrll unlikely to ever arise. Sometimes constructor symbols are in
942a6b7db3Sskrll unusual sections, such as the absolute section--this appears
952a6b7db3Sskrll to be the case in Linux a.out--and in such cases we just
962a6b7db3Sskrll assume everything is OK. */
972a6b7db3Sskrll if (p->elements != NULL
982a6b7db3Sskrll && section->owner != NULL
992a6b7db3Sskrll && p->elements->section->owner != NULL
1002a6b7db3Sskrll && strcmp (bfd_get_target (section->owner),
1012a6b7db3Sskrll bfd_get_target (p->elements->section->owner)) != 0)
1022a6b7db3Sskrll {
1037c6f6726Schristos einfo (_("%X%P: different object file formats composing set %s\n"),
1042a6b7db3Sskrll h->root.string);
1052a6b7db3Sskrll return;
1062a6b7db3Sskrll }
1072a6b7db3Sskrll }
1082a6b7db3Sskrll
10932998b1cSchristos e = (struct set_element *) xmalloc (sizeof (struct set_element));
110106c59e5Schristos e->u.next = NULL;
1112a6b7db3Sskrll e->name = name;
1122a6b7db3Sskrll e->section = section;
1132a6b7db3Sskrll e->value = value;
1142a6b7db3Sskrll
115106c59e5Schristos for (epp = &p->elements; *epp != NULL; epp = &(*epp)->u.next)
1162a6b7db3Sskrll ;
1172a6b7db3Sskrll *epp = e;
1182a6b7db3Sskrll
1192a6b7db3Sskrll ++p->count;
1202a6b7db3Sskrll }
1212a6b7db3Sskrll
1222a6b7db3Sskrll /* Get the priority of a g++ global constructor or destructor from the
1232a6b7db3Sskrll symbol name. */
1242a6b7db3Sskrll
1252a6b7db3Sskrll static int
ctor_prio(const char * name)1262a6b7db3Sskrll ctor_prio (const char *name)
1272a6b7db3Sskrll {
1282a6b7db3Sskrll /* The name will look something like _GLOBAL_$I$65535$test02__Fv.
1292a6b7db3Sskrll There might be extra leading underscores, and the $ characters
1302a6b7db3Sskrll might be something else. The I might be a D. */
1312a6b7db3Sskrll
1322a6b7db3Sskrll while (*name == '_')
1332a6b7db3Sskrll ++name;
1342a6b7db3Sskrll
13503f5171aSchristos if (!startswith (name, "GLOBAL_"))
1362a6b7db3Sskrll return -1;
1372a6b7db3Sskrll
1382a6b7db3Sskrll name += sizeof "GLOBAL_" - 1;
1392a6b7db3Sskrll
1402a6b7db3Sskrll if (name[0] != name[2])
1412a6b7db3Sskrll return -1;
1422a6b7db3Sskrll if (name[1] != 'I' && name[1] != 'D')
1432a6b7db3Sskrll return -1;
1442a6b7db3Sskrll if (!ISDIGIT (name[3]))
1452a6b7db3Sskrll return -1;
1462a6b7db3Sskrll
1472a6b7db3Sskrll return atoi (name + 3);
1482a6b7db3Sskrll }
1492a6b7db3Sskrll
1502a6b7db3Sskrll /* This function is used to sort constructor elements by priority. It
1512a6b7db3Sskrll is called via qsort. */
1522a6b7db3Sskrll
1532a6b7db3Sskrll static int
ctor_cmp(const void * p1,const void * p2)1542a6b7db3Sskrll ctor_cmp (const void *p1, const void *p2)
1552a6b7db3Sskrll {
156106c59e5Schristos const struct set_element *pe1 = *(const struct set_element **) p1;
157106c59e5Schristos const struct set_element *pe2 = *(const struct set_element **) p2;
1582a6b7db3Sskrll const char *n1;
1592a6b7db3Sskrll const char *n2;
1602a6b7db3Sskrll int prio1;
1612a6b7db3Sskrll int prio2;
1622a6b7db3Sskrll
163106c59e5Schristos n1 = pe1->name;
1642a6b7db3Sskrll if (n1 == NULL)
1652a6b7db3Sskrll n1 = "";
166106c59e5Schristos n2 = pe2->name;
1672a6b7db3Sskrll if (n2 == NULL)
1682a6b7db3Sskrll n2 = "";
1692a6b7db3Sskrll
1702a6b7db3Sskrll /* We need to sort in reverse order by priority. When two
1712a6b7db3Sskrll constructors have the same priority, we should maintain their
1722a6b7db3Sskrll current relative position. */
1732a6b7db3Sskrll
1742a6b7db3Sskrll prio1 = ctor_prio (n1);
1752a6b7db3Sskrll prio2 = ctor_prio (n2);
1762a6b7db3Sskrll
1772a6b7db3Sskrll /* We sort in reverse order because that is what g++ expects. */
1782a6b7db3Sskrll if (prio1 < prio2)
1792a6b7db3Sskrll return 1;
180106c59e5Schristos if (prio1 > prio2)
1812a6b7db3Sskrll return -1;
1822a6b7db3Sskrll
1832a6b7db3Sskrll /* Force a stable sort. */
184106c59e5Schristos if (pe1->u.idx < pe2->u.idx)
1852a6b7db3Sskrll return -1;
186106c59e5Schristos if (pe1->u.idx > pe2->u.idx)
1872a6b7db3Sskrll return 1;
1882a6b7db3Sskrll return 0;
1892a6b7db3Sskrll }
1902a6b7db3Sskrll
1912a6b7db3Sskrll /* This function is called after the first phase of the link and
1922a6b7db3Sskrll before the second phase. At this point all set information has
1932a6b7db3Sskrll been gathered. We now put the statements to build the sets
1942a6b7db3Sskrll themselves into constructor_list. */
1952a6b7db3Sskrll
1962a6b7db3Sskrll void
ldctor_build_sets(void)1972a6b7db3Sskrll ldctor_build_sets (void)
1982a6b7db3Sskrll {
19903f5171aSchristos static bool called;
20003f5171aSchristos bool header_printed;
2012a6b7db3Sskrll struct set_info *p;
2022a6b7db3Sskrll
2032a6b7db3Sskrll /* The emulation code may call us directly, but we only want to do
2042a6b7db3Sskrll this once. */
2052a6b7db3Sskrll if (called)
2062a6b7db3Sskrll return;
20703f5171aSchristos called = true;
2082a6b7db3Sskrll
2092a6b7db3Sskrll if (constructors_sorted)
2102a6b7db3Sskrll {
2112a6b7db3Sskrll for (p = sets; p != NULL; p = p->next)
2122a6b7db3Sskrll {
2132a6b7db3Sskrll int c, i;
214106c59e5Schristos struct set_element *e, *enext;
2152a6b7db3Sskrll struct set_element **array;
2162a6b7db3Sskrll
2172a6b7db3Sskrll if (p->elements == NULL)
2182a6b7db3Sskrll continue;
2192a6b7db3Sskrll
2202a6b7db3Sskrll c = 0;
221106c59e5Schristos for (e = p->elements; e != NULL; e = e->u.next)
2222a6b7db3Sskrll ++c;
2232a6b7db3Sskrll
22432998b1cSchristos array = (struct set_element **) xmalloc (c * sizeof *array);
2252a6b7db3Sskrll
2262a6b7db3Sskrll i = 0;
227106c59e5Schristos for (e = p->elements; e != NULL; e = enext)
2282a6b7db3Sskrll {
2292a6b7db3Sskrll array[i] = e;
230106c59e5Schristos enext = e->u.next;
231106c59e5Schristos e->u.idx = i;
2322a6b7db3Sskrll ++i;
2332a6b7db3Sskrll }
2342a6b7db3Sskrll
2352a6b7db3Sskrll qsort (array, c, sizeof *array, ctor_cmp);
2362a6b7db3Sskrll
2372a6b7db3Sskrll e = array[0];
2382a6b7db3Sskrll p->elements = e;
2392a6b7db3Sskrll for (i = 0; i < c - 1; i++)
240106c59e5Schristos array[i]->u.next = array[i + 1];
241106c59e5Schristos array[i]->u.next = NULL;
2422a6b7db3Sskrll
2432a6b7db3Sskrll free (array);
2442a6b7db3Sskrll }
2452a6b7db3Sskrll }
2462a6b7db3Sskrll
247af515df4Sskrll lang_list_init (&constructor_list);
248af515df4Sskrll push_stat_ptr (&constructor_list);
2492a6b7db3Sskrll
25003f5171aSchristos header_printed = false;
2512a6b7db3Sskrll for (p = sets; p != NULL; p = p->next)
2522a6b7db3Sskrll {
2532a6b7db3Sskrll struct set_element *e;
2542a6b7db3Sskrll reloc_howto_type *howto;
2552a6b7db3Sskrll int reloc_size, size;
2562a6b7db3Sskrll
2572a6b7db3Sskrll /* If the symbol is defined, we may have been invoked from
2582a6b7db3Sskrll collect, and the sets may already have been built, so we do
2592a6b7db3Sskrll not do anything. */
2602a6b7db3Sskrll if (p->h->type == bfd_link_hash_defined
2612a6b7db3Sskrll || p->h->type == bfd_link_hash_defweak)
2622a6b7db3Sskrll continue;
2632a6b7db3Sskrll
2642a6b7db3Sskrll /* For each set we build:
2652a6b7db3Sskrll set:
2662a6b7db3Sskrll .long number_of_elements
2672a6b7db3Sskrll .long element0
2682a6b7db3Sskrll ...
2692a6b7db3Sskrll .long elementN
2702a6b7db3Sskrll .long 0
2712a6b7db3Sskrll except that we use the right size instead of .long. When
2722a6b7db3Sskrll generating relocatable output, we generate relocs instead of
2732a6b7db3Sskrll addresses. */
2742a6b7db3Sskrll howto = bfd_reloc_type_lookup (link_info.output_bfd, p->reloc);
2752a6b7db3Sskrll if (howto == NULL)
2762a6b7db3Sskrll {
2775f4eaf39Schristos if (bfd_link_relocatable (&link_info))
2782a6b7db3Sskrll {
2797c6f6726Schristos einfo (_("%X%P: %s does not support reloc %s for set %s\n"),
2802a6b7db3Sskrll bfd_get_target (link_info.output_bfd),
2812a6b7db3Sskrll bfd_get_reloc_code_name (p->reloc),
2822a6b7db3Sskrll p->h->root.string);
2832a6b7db3Sskrll continue;
2842a6b7db3Sskrll }
2852a6b7db3Sskrll
2862a6b7db3Sskrll /* If this is not a relocatable link, all we need is the
2872a6b7db3Sskrll size, which we can get from the input BFD. */
2882a6b7db3Sskrll if (p->elements->section->owner != NULL)
2892a6b7db3Sskrll howto = bfd_reloc_type_lookup (p->elements->section->owner,
2902a6b7db3Sskrll p->reloc);
2912a6b7db3Sskrll if (howto == NULL)
2922a6b7db3Sskrll {
2936f226886Schristos /* See PR 20911 for a reproducer. */
2946f226886Schristos if (p->elements->section->owner == NULL)
2957c6f6726Schristos einfo (_("%X%P: special section %s does not support reloc %s for set %s\n"),
296106c59e5Schristos bfd_section_name (p->elements->section),
2976f226886Schristos bfd_get_reloc_code_name (p->reloc),
2986f226886Schristos p->h->root.string);
2996f226886Schristos else
3007c6f6726Schristos einfo (_("%X%P: %s does not support reloc %s for set %s\n"),
3012a6b7db3Sskrll bfd_get_target (p->elements->section->owner),
3022a6b7db3Sskrll bfd_get_reloc_code_name (p->reloc),
3032a6b7db3Sskrll p->h->root.string);
3042a6b7db3Sskrll continue;
3052a6b7db3Sskrll }
3062a6b7db3Sskrll }
3072a6b7db3Sskrll
3082a6b7db3Sskrll reloc_size = bfd_get_reloc_size (howto);
3092a6b7db3Sskrll switch (reloc_size)
3102a6b7db3Sskrll {
3112a6b7db3Sskrll case 1: size = BYTE; break;
3122a6b7db3Sskrll case 2: size = SHORT; break;
3132a6b7db3Sskrll case 4: size = LONG; break;
3142a6b7db3Sskrll case 8:
3152a6b7db3Sskrll if (howto->complain_on_overflow == complain_overflow_signed)
3162a6b7db3Sskrll size = SQUAD;
3172a6b7db3Sskrll else
3182a6b7db3Sskrll size = QUAD;
3192a6b7db3Sskrll break;
3202a6b7db3Sskrll default:
3217c6f6726Schristos einfo (_("%X%P: unsupported size %d for set %s\n"),
3222a6b7db3Sskrll bfd_get_reloc_size (howto), p->h->root.string);
3232a6b7db3Sskrll size = LONG;
3242a6b7db3Sskrll break;
3252a6b7db3Sskrll }
3262a6b7db3Sskrll
32732998b1cSchristos lang_add_assignment (exp_assign (".",
3282a6b7db3Sskrll exp_unop (ALIGN_K,
329b410d692Schristos exp_intop (reloc_size)),
33003f5171aSchristos false));
33132998b1cSchristos lang_add_assignment (exp_assign (p->h->root.string,
332b410d692Schristos exp_nameop (NAME, "."),
33303f5171aSchristos false));
3342a6b7db3Sskrll lang_add_data (size, exp_intop (p->count));
3352a6b7db3Sskrll
336106c59e5Schristos for (e = p->elements; e != NULL; e = e->u.next)
3372a6b7db3Sskrll {
3382a6b7db3Sskrll if (config.map_file != NULL)
3392a6b7db3Sskrll {
3402a6b7db3Sskrll int len;
3412a6b7db3Sskrll
3422a6b7db3Sskrll if (!header_printed)
3432a6b7db3Sskrll {
3442a6b7db3Sskrll minfo (_("\nSet Symbol\n\n"));
34503f5171aSchristos header_printed = true;
3462a6b7db3Sskrll }
3472a6b7db3Sskrll
3482a6b7db3Sskrll minfo ("%s", p->h->root.string);
3492a6b7db3Sskrll len = strlen (p->h->root.string);
3502a6b7db3Sskrll
3512a6b7db3Sskrll if (len >= 19)
3522a6b7db3Sskrll {
3532a6b7db3Sskrll print_nl ();
3542a6b7db3Sskrll len = 0;
3552a6b7db3Sskrll }
356*dd7241dfSchristos print_spaces (20 - len);
3572a6b7db3Sskrll
3582a6b7db3Sskrll if (e->name != NULL)
3597c6f6726Schristos minfo ("%pT\n", e->name);
3602a6b7db3Sskrll else
3612a6b7db3Sskrll minfo ("%G\n", e->section->owner, e->section, e->value);
3622a6b7db3Sskrll }
3632a6b7db3Sskrll
3642a6b7db3Sskrll /* Need SEC_KEEP for --gc-sections. */
3652a6b7db3Sskrll if (!bfd_is_abs_section (e->section))
3662a6b7db3Sskrll e->section->flags |= SEC_KEEP;
3672a6b7db3Sskrll
3685f4eaf39Schristos if (bfd_link_relocatable (&link_info))
3692a6b7db3Sskrll lang_add_reloc (p->reloc, howto, e->section, e->name,
3702a6b7db3Sskrll exp_intop (e->value));
3712a6b7db3Sskrll else
3722a6b7db3Sskrll lang_add_data (size, exp_relop (e->section, e->value));
3732a6b7db3Sskrll }
3742a6b7db3Sskrll
3752a6b7db3Sskrll lang_add_data (size, exp_intop (0));
3762a6b7db3Sskrll }
3772a6b7db3Sskrll
378af515df4Sskrll pop_stat_ptr ();
3792a6b7db3Sskrll }
380