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