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