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