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