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