1*56bb7041Schristos /* ldctor.c -- constructor support routines
2*56bb7041Schristos Copyright (C) 1991-2020 Free Software Foundation, Inc.
3*56bb7041Schristos By Steve Chamberlain <sac@cygnus.com>
4*56bb7041Schristos
5*56bb7041Schristos This file is part of the GNU Binutils.
6*56bb7041Schristos
7*56bb7041Schristos This program is free software; you can redistribute it and/or modify
8*56bb7041Schristos it under the terms of the GNU General Public License as published by
9*56bb7041Schristos the Free Software Foundation; either version 3 of the License, or
10*56bb7041Schristos (at your option) any later version.
11*56bb7041Schristos
12*56bb7041Schristos This program is distributed in the hope that it will be useful,
13*56bb7041Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
14*56bb7041Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*56bb7041Schristos GNU General Public License for more details.
16*56bb7041Schristos
17*56bb7041Schristos You should have received a copy of the GNU General Public License
18*56bb7041Schristos along with this program; if not, write to the Free Software
19*56bb7041Schristos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20*56bb7041Schristos MA 02110-1301, USA. */
21*56bb7041Schristos
22*56bb7041Schristos #include "sysdep.h"
23*56bb7041Schristos #include "bfd.h"
24*56bb7041Schristos #include "bfdlink.h"
25*56bb7041Schristos #include "safe-ctype.h"
26*56bb7041Schristos #include "ctf-api.h"
27*56bb7041Schristos
28*56bb7041Schristos #include "ld.h"
29*56bb7041Schristos #include "ldexp.h"
30*56bb7041Schristos #include "ldlang.h"
31*56bb7041Schristos #include "ldmisc.h"
32*56bb7041Schristos #include <ldgram.h>
33*56bb7041Schristos #include "ldmain.h"
34*56bb7041Schristos #include "ldctor.h"
35*56bb7041Schristos
36*56bb7041Schristos /* The list of statements needed to handle constructors. These are
37*56bb7041Schristos invoked by the command CONSTRUCTORS in the linker script. */
38*56bb7041Schristos lang_statement_list_type constructor_list;
39*56bb7041Schristos
40*56bb7041Schristos /* Whether the constructors should be sorted. Note that this is
41*56bb7041Schristos global for the entire link; we assume that there is only a single
42*56bb7041Schristos CONSTRUCTORS command in the linker script. */
43*56bb7041Schristos bfd_boolean constructors_sorted;
44*56bb7041Schristos
45*56bb7041Schristos /* The sets we have seen. */
46*56bb7041Schristos struct set_info *sets;
47*56bb7041Schristos
48*56bb7041Schristos /* Add an entry to a set. H is the entry in the linker hash table.
49*56bb7041Schristos RELOC is the relocation to use for an entry in the set. SECTION
50*56bb7041Schristos and VALUE are the value to add. This is called during the first
51*56bb7041Schristos phase of the link, when we are still gathering symbols together.
52*56bb7041Schristos We just record the information now. The ldctor_build_sets
53*56bb7041Schristos function will construct the sets. */
54*56bb7041Schristos
55*56bb7041Schristos 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)56*56bb7041Schristos ldctor_add_set_entry (struct bfd_link_hash_entry *h,
57*56bb7041Schristos bfd_reloc_code_real_type reloc,
58*56bb7041Schristos const char *name,
59*56bb7041Schristos asection *section,
60*56bb7041Schristos bfd_vma value)
61*56bb7041Schristos {
62*56bb7041Schristos struct set_info *p;
63*56bb7041Schristos struct set_element *e;
64*56bb7041Schristos struct set_element **epp;
65*56bb7041Schristos
66*56bb7041Schristos for (p = sets; p != NULL; p = p->next)
67*56bb7041Schristos if (p->h == h)
68*56bb7041Schristos break;
69*56bb7041Schristos
70*56bb7041Schristos if (p == NULL)
71*56bb7041Schristos {
72*56bb7041Schristos p = (struct set_info *) xmalloc (sizeof (struct set_info));
73*56bb7041Schristos p->next = sets;
74*56bb7041Schristos sets = p;
75*56bb7041Schristos p->h = h;
76*56bb7041Schristos p->reloc = reloc;
77*56bb7041Schristos p->count = 0;
78*56bb7041Schristos p->elements = NULL;
79*56bb7041Schristos }
80*56bb7041Schristos else
81*56bb7041Schristos {
82*56bb7041Schristos if (p->reloc != reloc)
83*56bb7041Schristos {
84*56bb7041Schristos einfo (_("%X%P: different relocs used in set %s\n"),
85*56bb7041Schristos h->root.string);
86*56bb7041Schristos return;
87*56bb7041Schristos }
88*56bb7041Schristos
89*56bb7041Schristos /* Don't permit a set to be constructed from different object
90*56bb7041Schristos file formats. The same reloc may have different results. We
91*56bb7041Schristos actually could sometimes handle this, but the case is
92*56bb7041Schristos unlikely to ever arise. Sometimes constructor symbols are in
93*56bb7041Schristos unusual sections, such as the absolute section--this appears
94*56bb7041Schristos to be the case in Linux a.out--and in such cases we just
95*56bb7041Schristos assume everything is OK. */
96*56bb7041Schristos if (p->elements != NULL
97*56bb7041Schristos && section->owner != NULL
98*56bb7041Schristos && p->elements->section->owner != NULL
99*56bb7041Schristos && strcmp (bfd_get_target (section->owner),
100*56bb7041Schristos bfd_get_target (p->elements->section->owner)) != 0)
101*56bb7041Schristos {
102*56bb7041Schristos einfo (_("%X%P: different object file formats composing set %s\n"),
103*56bb7041Schristos h->root.string);
104*56bb7041Schristos return;
105*56bb7041Schristos }
106*56bb7041Schristos }
107*56bb7041Schristos
108*56bb7041Schristos e = (struct set_element *) xmalloc (sizeof (struct set_element));
109*56bb7041Schristos e->u.next = NULL;
110*56bb7041Schristos e->name = name;
111*56bb7041Schristos e->section = section;
112*56bb7041Schristos e->value = value;
113*56bb7041Schristos
114*56bb7041Schristos for (epp = &p->elements; *epp != NULL; epp = &(*epp)->u.next)
115*56bb7041Schristos ;
116*56bb7041Schristos *epp = e;
117*56bb7041Schristos
118*56bb7041Schristos ++p->count;
119*56bb7041Schristos }
120*56bb7041Schristos
121*56bb7041Schristos /* Get the priority of a g++ global constructor or destructor from the
122*56bb7041Schristos symbol name. */
123*56bb7041Schristos
124*56bb7041Schristos static int
ctor_prio(const char * name)125*56bb7041Schristos ctor_prio (const char *name)
126*56bb7041Schristos {
127*56bb7041Schristos /* The name will look something like _GLOBAL_$I$65535$test02__Fv.
128*56bb7041Schristos There might be extra leading underscores, and the $ characters
129*56bb7041Schristos might be something else. The I might be a D. */
130*56bb7041Schristos
131*56bb7041Schristos while (*name == '_')
132*56bb7041Schristos ++name;
133*56bb7041Schristos
134*56bb7041Schristos if (!CONST_STRNEQ (name, "GLOBAL_"))
135*56bb7041Schristos return -1;
136*56bb7041Schristos
137*56bb7041Schristos name += sizeof "GLOBAL_" - 1;
138*56bb7041Schristos
139*56bb7041Schristos if (name[0] != name[2])
140*56bb7041Schristos return -1;
141*56bb7041Schristos if (name[1] != 'I' && name[1] != 'D')
142*56bb7041Schristos return -1;
143*56bb7041Schristos if (!ISDIGIT (name[3]))
144*56bb7041Schristos return -1;
145*56bb7041Schristos
146*56bb7041Schristos return atoi (name + 3);
147*56bb7041Schristos }
148*56bb7041Schristos
149*56bb7041Schristos /* This function is used to sort constructor elements by priority. It
150*56bb7041Schristos is called via qsort. */
151*56bb7041Schristos
152*56bb7041Schristos static int
ctor_cmp(const void * p1,const void * p2)153*56bb7041Schristos ctor_cmp (const void *p1, const void *p2)
154*56bb7041Schristos {
155*56bb7041Schristos const struct set_element *pe1 = *(const struct set_element **) p1;
156*56bb7041Schristos const struct set_element *pe2 = *(const struct set_element **) p2;
157*56bb7041Schristos const char *n1;
158*56bb7041Schristos const char *n2;
159*56bb7041Schristos int prio1;
160*56bb7041Schristos int prio2;
161*56bb7041Schristos
162*56bb7041Schristos n1 = pe1->name;
163*56bb7041Schristos if (n1 == NULL)
164*56bb7041Schristos n1 = "";
165*56bb7041Schristos n2 = pe2->name;
166*56bb7041Schristos if (n2 == NULL)
167*56bb7041Schristos n2 = "";
168*56bb7041Schristos
169*56bb7041Schristos /* We need to sort in reverse order by priority. When two
170*56bb7041Schristos constructors have the same priority, we should maintain their
171*56bb7041Schristos current relative position. */
172*56bb7041Schristos
173*56bb7041Schristos prio1 = ctor_prio (n1);
174*56bb7041Schristos prio2 = ctor_prio (n2);
175*56bb7041Schristos
176*56bb7041Schristos /* We sort in reverse order because that is what g++ expects. */
177*56bb7041Schristos if (prio1 < prio2)
178*56bb7041Schristos return 1;
179*56bb7041Schristos if (prio1 > prio2)
180*56bb7041Schristos return -1;
181*56bb7041Schristos
182*56bb7041Schristos /* Force a stable sort. */
183*56bb7041Schristos if (pe1->u.idx < pe2->u.idx)
184*56bb7041Schristos return -1;
185*56bb7041Schristos if (pe1->u.idx > pe2->u.idx)
186*56bb7041Schristos return 1;
187*56bb7041Schristos return 0;
188*56bb7041Schristos }
189*56bb7041Schristos
190*56bb7041Schristos /* This function is called after the first phase of the link and
191*56bb7041Schristos before the second phase. At this point all set information has
192*56bb7041Schristos been gathered. We now put the statements to build the sets
193*56bb7041Schristos themselves into constructor_list. */
194*56bb7041Schristos
195*56bb7041Schristos void
ldctor_build_sets(void)196*56bb7041Schristos ldctor_build_sets (void)
197*56bb7041Schristos {
198*56bb7041Schristos static bfd_boolean called;
199*56bb7041Schristos bfd_boolean header_printed;
200*56bb7041Schristos struct set_info *p;
201*56bb7041Schristos
202*56bb7041Schristos /* The emulation code may call us directly, but we only want to do
203*56bb7041Schristos this once. */
204*56bb7041Schristos if (called)
205*56bb7041Schristos return;
206*56bb7041Schristos called = TRUE;
207*56bb7041Schristos
208*56bb7041Schristos if (constructors_sorted)
209*56bb7041Schristos {
210*56bb7041Schristos for (p = sets; p != NULL; p = p->next)
211*56bb7041Schristos {
212*56bb7041Schristos int c, i;
213*56bb7041Schristos struct set_element *e, *enext;
214*56bb7041Schristos struct set_element **array;
215*56bb7041Schristos
216*56bb7041Schristos if (p->elements == NULL)
217*56bb7041Schristos continue;
218*56bb7041Schristos
219*56bb7041Schristos c = 0;
220*56bb7041Schristos for (e = p->elements; e != NULL; e = e->u.next)
221*56bb7041Schristos ++c;
222*56bb7041Schristos
223*56bb7041Schristos array = (struct set_element **) xmalloc (c * sizeof *array);
224*56bb7041Schristos
225*56bb7041Schristos i = 0;
226*56bb7041Schristos for (e = p->elements; e != NULL; e = enext)
227*56bb7041Schristos {
228*56bb7041Schristos array[i] = e;
229*56bb7041Schristos enext = e->u.next;
230*56bb7041Schristos e->u.idx = i;
231*56bb7041Schristos ++i;
232*56bb7041Schristos }
233*56bb7041Schristos
234*56bb7041Schristos qsort (array, c, sizeof *array, ctor_cmp);
235*56bb7041Schristos
236*56bb7041Schristos e = array[0];
237*56bb7041Schristos p->elements = e;
238*56bb7041Schristos for (i = 0; i < c - 1; i++)
239*56bb7041Schristos array[i]->u.next = array[i + 1];
240*56bb7041Schristos array[i]->u.next = NULL;
241*56bb7041Schristos
242*56bb7041Schristos free (array);
243*56bb7041Schristos }
244*56bb7041Schristos }
245*56bb7041Schristos
246*56bb7041Schristos lang_list_init (&constructor_list);
247*56bb7041Schristos push_stat_ptr (&constructor_list);
248*56bb7041Schristos
249*56bb7041Schristos header_printed = FALSE;
250*56bb7041Schristos for (p = sets; p != NULL; p = p->next)
251*56bb7041Schristos {
252*56bb7041Schristos struct set_element *e;
253*56bb7041Schristos reloc_howto_type *howto;
254*56bb7041Schristos int reloc_size, size;
255*56bb7041Schristos
256*56bb7041Schristos /* If the symbol is defined, we may have been invoked from
257*56bb7041Schristos collect, and the sets may already have been built, so we do
258*56bb7041Schristos not do anything. */
259*56bb7041Schristos if (p->h->type == bfd_link_hash_defined
260*56bb7041Schristos || p->h->type == bfd_link_hash_defweak)
261*56bb7041Schristos continue;
262*56bb7041Schristos
263*56bb7041Schristos /* For each set we build:
264*56bb7041Schristos set:
265*56bb7041Schristos .long number_of_elements
266*56bb7041Schristos .long element0
267*56bb7041Schristos ...
268*56bb7041Schristos .long elementN
269*56bb7041Schristos .long 0
270*56bb7041Schristos except that we use the right size instead of .long. When
271*56bb7041Schristos generating relocatable output, we generate relocs instead of
272*56bb7041Schristos addresses. */
273*56bb7041Schristos howto = bfd_reloc_type_lookup (link_info.output_bfd, p->reloc);
274*56bb7041Schristos if (howto == NULL)
275*56bb7041Schristos {
276*56bb7041Schristos if (bfd_link_relocatable (&link_info))
277*56bb7041Schristos {
278*56bb7041Schristos einfo (_("%X%P: %s does not support reloc %s for set %s\n"),
279*56bb7041Schristos bfd_get_target (link_info.output_bfd),
280*56bb7041Schristos bfd_get_reloc_code_name (p->reloc),
281*56bb7041Schristos p->h->root.string);
282*56bb7041Schristos continue;
283*56bb7041Schristos }
284*56bb7041Schristos
285*56bb7041Schristos /* If this is not a relocatable link, all we need is the
286*56bb7041Schristos size, which we can get from the input BFD. */
287*56bb7041Schristos if (p->elements->section->owner != NULL)
288*56bb7041Schristos howto = bfd_reloc_type_lookup (p->elements->section->owner,
289*56bb7041Schristos p->reloc);
290*56bb7041Schristos if (howto == NULL)
291*56bb7041Schristos {
292*56bb7041Schristos /* See PR 20911 for a reproducer. */
293*56bb7041Schristos if (p->elements->section->owner == NULL)
294*56bb7041Schristos einfo (_("%X%P: special section %s does not support reloc %s for set %s\n"),
295*56bb7041Schristos bfd_section_name (p->elements->section),
296*56bb7041Schristos bfd_get_reloc_code_name (p->reloc),
297*56bb7041Schristos p->h->root.string);
298*56bb7041Schristos else
299*56bb7041Schristos einfo (_("%X%P: %s does not support reloc %s for set %s\n"),
300*56bb7041Schristos bfd_get_target (p->elements->section->owner),
301*56bb7041Schristos bfd_get_reloc_code_name (p->reloc),
302*56bb7041Schristos p->h->root.string);
303*56bb7041Schristos continue;
304*56bb7041Schristos }
305*56bb7041Schristos }
306*56bb7041Schristos
307*56bb7041Schristos reloc_size = bfd_get_reloc_size (howto);
308*56bb7041Schristos switch (reloc_size)
309*56bb7041Schristos {
310*56bb7041Schristos case 1: size = BYTE; break;
311*56bb7041Schristos case 2: size = SHORT; break;
312*56bb7041Schristos case 4: size = LONG; break;
313*56bb7041Schristos case 8:
314*56bb7041Schristos if (howto->complain_on_overflow == complain_overflow_signed)
315*56bb7041Schristos size = SQUAD;
316*56bb7041Schristos else
317*56bb7041Schristos size = QUAD;
318*56bb7041Schristos break;
319*56bb7041Schristos default:
320*56bb7041Schristos einfo (_("%X%P: unsupported size %d for set %s\n"),
321*56bb7041Schristos bfd_get_reloc_size (howto), p->h->root.string);
322*56bb7041Schristos size = LONG;
323*56bb7041Schristos break;
324*56bb7041Schristos }
325*56bb7041Schristos
326*56bb7041Schristos lang_add_assignment (exp_assign (".",
327*56bb7041Schristos exp_unop (ALIGN_K,
328*56bb7041Schristos exp_intop (reloc_size)),
329*56bb7041Schristos FALSE));
330*56bb7041Schristos lang_add_assignment (exp_assign (p->h->root.string,
331*56bb7041Schristos exp_nameop (NAME, "."),
332*56bb7041Schristos FALSE));
333*56bb7041Schristos lang_add_data (size, exp_intop (p->count));
334*56bb7041Schristos
335*56bb7041Schristos for (e = p->elements; e != NULL; e = e->u.next)
336*56bb7041Schristos {
337*56bb7041Schristos if (config.map_file != NULL)
338*56bb7041Schristos {
339*56bb7041Schristos int len;
340*56bb7041Schristos
341*56bb7041Schristos if (!header_printed)
342*56bb7041Schristos {
343*56bb7041Schristos minfo (_("\nSet Symbol\n\n"));
344*56bb7041Schristos header_printed = TRUE;
345*56bb7041Schristos }
346*56bb7041Schristos
347*56bb7041Schristos minfo ("%s", p->h->root.string);
348*56bb7041Schristos len = strlen (p->h->root.string);
349*56bb7041Schristos
350*56bb7041Schristos if (len >= 19)
351*56bb7041Schristos {
352*56bb7041Schristos print_nl ();
353*56bb7041Schristos len = 0;
354*56bb7041Schristos }
355*56bb7041Schristos while (len < 20)
356*56bb7041Schristos {
357*56bb7041Schristos print_space ();
358*56bb7041Schristos ++len;
359*56bb7041Schristos }
360*56bb7041Schristos
361*56bb7041Schristos if (e->name != NULL)
362*56bb7041Schristos minfo ("%pT\n", e->name);
363*56bb7041Schristos else
364*56bb7041Schristos minfo ("%G\n", e->section->owner, e->section, e->value);
365*56bb7041Schristos }
366*56bb7041Schristos
367*56bb7041Schristos /* Need SEC_KEEP for --gc-sections. */
368*56bb7041Schristos if (!bfd_is_abs_section (e->section))
369*56bb7041Schristos e->section->flags |= SEC_KEEP;
370*56bb7041Schristos
371*56bb7041Schristos if (bfd_link_relocatable (&link_info))
372*56bb7041Schristos lang_add_reloc (p->reloc, howto, e->section, e->name,
373*56bb7041Schristos exp_intop (e->value));
374*56bb7041Schristos else
375*56bb7041Schristos lang_add_data (size, exp_relop (e->section, e->value));
376*56bb7041Schristos }
377*56bb7041Schristos
378*56bb7041Schristos lang_add_data (size, exp_intop (0));
379*56bb7041Schristos }
380*56bb7041Schristos
381*56bb7041Schristos pop_stat_ptr ();
382*56bb7041Schristos }
383