]>
Commit | Line | Data |
---|---|---|
d871d478 | 1 | /* Emulation code used by all ELF targets. |
a2c58332 | 2 | Copyright (C) 1991-2022 Free Software Foundation, Inc. |
d871d478 AM |
3 | |
4 | This file is part of the GNU Binutils. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 3 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
19 | MA 02110-1301, USA. */ | |
20 | ||
21 | #include "sysdep.h" | |
22 | #include "bfd.h" | |
0b4453c7 | 23 | #include "bfdlink.h" |
1ff6de03 | 24 | #include "ctf-api.h" |
d871d478 AM |
25 | #include "ld.h" |
26 | #include "ldmain.h" | |
27 | #include "ldmisc.h" | |
28 | #include "ldexp.h" | |
29 | #include "ldlang.h" | |
b209b5a6 | 30 | #include "ldctor.h" |
d871d478 | 31 | #include "elf-bfd.h" |
3d16b64e | 32 | #include "elf/internal.h" |
d871d478 AM |
33 | #include "ldelfgen.h" |
34 | ||
b209b5a6 AM |
35 | /* Info attached to an output_section_statement about input sections, |
36 | used when sorting SHF_LINK_ORDER sections. */ | |
37 | ||
38 | struct os_sections | |
39 | { | |
40 | /* Size allocated for isec. */ | |
41 | unsigned int alloc; | |
42 | /* Used entries in isec. */ | |
43 | unsigned int count; | |
44 | /* How many are SHF_LINK_ORDER. */ | |
45 | unsigned int ordered; | |
46 | /* Input sections attached to this output section. */ | |
47 | struct os_sections_input { | |
48 | lang_input_section_type *is; | |
49 | unsigned int idx; | |
50 | } isec[1]; | |
51 | }; | |
52 | ||
53 | /* Add IS to data kept for OS. */ | |
54 | ||
f38a2680 | 55 | static bool |
b209b5a6 AM |
56 | add_link_order_input_section (lang_input_section_type *is, |
57 | lang_output_section_statement_type *os) | |
58 | { | |
59 | struct os_sections *os_info = os->data; | |
60 | asection *s; | |
61 | ||
62 | if (os_info == NULL) | |
63 | { | |
64 | os_info = xmalloc (sizeof (*os_info) + 63 * sizeof (*os_info->isec)); | |
65 | os_info->alloc = 64; | |
66 | os_info->count = 0; | |
67 | os_info->ordered = 0; | |
68 | os->data = os_info; | |
69 | } | |
70 | if (os_info->count == os_info->alloc) | |
71 | { | |
72 | size_t want; | |
73 | os_info->alloc *= 2; | |
74 | want = sizeof (*os_info) + (os_info->alloc - 1) * sizeof (*os_info->isec); | |
75 | os_info = xrealloc (os_info, want); | |
76 | os->data = os_info; | |
77 | } | |
78 | os_info->isec[os_info->count].is = is; | |
79 | os_info->isec[os_info->count].idx = os_info->count; | |
80 | os_info->count++; | |
81 | s = is->section; | |
4120e488 | 82 | if (bfd_get_flavour (s->owner) == bfd_target_elf_flavour |
b634d11d | 83 | && (s->flags & SEC_LINKER_CREATED) == 0 |
b209b5a6 AM |
84 | && elf_linked_to_section (s) != NULL) |
85 | os_info->ordered++; | |
f38a2680 | 86 | return false; |
b209b5a6 AM |
87 | } |
88 | ||
89 | /* Run over the linker's statement list, extracting info about input | |
90 | sections attached to each output section. */ | |
91 | ||
f38a2680 | 92 | static bool |
b209b5a6 AM |
93 | link_order_scan (lang_statement_union_type *u, |
94 | lang_output_section_statement_type *os) | |
95 | { | |
96 | asection *s; | |
f38a2680 | 97 | bool ret = false; |
b209b5a6 AM |
98 | |
99 | for (; u != NULL; u = u->header.next) | |
100 | { | |
101 | switch (u->header.type) | |
102 | { | |
103 | case lang_wild_statement_enum: | |
104 | if (link_order_scan (u->wild_statement.children.head, os)) | |
f38a2680 | 105 | ret = true; |
b209b5a6 AM |
106 | break; |
107 | case lang_constructors_statement_enum: | |
108 | if (link_order_scan (constructor_list.head, os)) | |
f38a2680 | 109 | ret = true; |
b209b5a6 AM |
110 | break; |
111 | case lang_output_section_statement_enum: | |
112 | if (u->output_section_statement.constraint != -1 | |
113 | && link_order_scan (u->output_section_statement.children.head, | |
114 | &u->output_section_statement)) | |
f38a2680 | 115 | ret = true; |
b209b5a6 AM |
116 | break; |
117 | case lang_group_statement_enum: | |
118 | if (link_order_scan (u->group_statement.children.head, os)) | |
f38a2680 | 119 | ret = true; |
b209b5a6 AM |
120 | break; |
121 | case lang_input_section_enum: | |
122 | s = u->input_section.section; | |
123 | if (s->output_section != NULL | |
124 | && s->output_section->owner == link_info.output_bfd | |
125 | && (s->output_section->flags & SEC_EXCLUDE) == 0 | |
126 | && ((s->output_section->flags & SEC_HAS_CONTENTS) != 0 | |
127 | || ((s->output_section->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) | |
128 | == (SEC_LOAD | SEC_THREAD_LOCAL)))) | |
129 | if (add_link_order_input_section (&u->input_section, os)) | |
f38a2680 | 130 | ret = true; |
b209b5a6 AM |
131 | break; |
132 | default: | |
133 | break; | |
134 | } | |
135 | } | |
136 | return ret; | |
137 | } | |
138 | ||
139 | /* Compare two sections based on the locations of the sections they are | |
140 | linked to. Used by fixup_link_order. */ | |
141 | ||
142 | static int | |
143 | compare_link_order (const void *a, const void *b) | |
144 | { | |
145 | const struct os_sections_input *ai = a; | |
146 | const struct os_sections_input *bi = b; | |
4120e488 AM |
147 | asection *asec = NULL; |
148 | asection *bsec = NULL; | |
b209b5a6 AM |
149 | bfd_vma apos, bpos; |
150 | ||
4120e488 AM |
151 | if (bfd_get_flavour (ai->is->section->owner) == bfd_target_elf_flavour) |
152 | asec = elf_linked_to_section (ai->is->section); | |
153 | if (bfd_get_flavour (bi->is->section->owner) == bfd_target_elf_flavour) | |
154 | bsec = elf_linked_to_section (bi->is->section); | |
155 | ||
b209b5a6 AM |
156 | /* Place unordered sections before ordered sections. */ |
157 | if (asec == NULL || bsec == NULL) | |
158 | { | |
159 | if (bsec != NULL) | |
160 | return -1; | |
161 | else if (asec != NULL) | |
162 | return 1; | |
163 | return ai->idx - bi->idx; | |
164 | } | |
165 | ||
166 | apos = asec->output_section->lma + asec->output_offset; | |
167 | bpos = bsec->output_section->lma + bsec->output_offset; | |
168 | ||
169 | if (apos < bpos) | |
170 | return -1; | |
171 | else if (apos > bpos) | |
172 | return 1; | |
173 | ||
d820a652 NC |
174 | if (! bfd_link_relocatable (&link_info)) |
175 | { | |
6628ac00 SL |
176 | /* The only way we should get matching LMAs is when the first of |
177 | the two sections has zero size, or asec and bsec are the | |
178 | same section. */ | |
d820a652 NC |
179 | if (asec->size < bsec->size) |
180 | return -1; | |
181 | else if (asec->size > bsec->size) | |
182 | return 1; | |
183 | } | |
b209b5a6 AM |
184 | |
185 | /* If they are both zero size then they almost certainly have the same | |
186 | VMA and thus are not ordered with respect to each other. Test VMA | |
6628ac00 | 187 | anyway, and fall back to idx to make the result reproducible across |
b209b5a6 AM |
188 | qsort implementations. */ |
189 | apos = asec->output_section->vma + asec->output_offset; | |
190 | bpos = bsec->output_section->vma + bsec->output_offset; | |
191 | if (apos < bpos) | |
192 | return -1; | |
193 | else if (apos > bpos) | |
194 | return 1; | |
6628ac00 SL |
195 | else |
196 | return ai->idx - bi->idx; | |
b209b5a6 AM |
197 | } |
198 | ||
199 | /* Rearrange sections with SHF_LINK_ORDER into the same order as their | |
200 | linked sections. */ | |
201 | ||
f38a2680 | 202 | static bool |
b209b5a6 AM |
203 | fixup_link_order (lang_output_section_statement_type *os) |
204 | { | |
205 | struct os_sections *os_info = os->data; | |
206 | unsigned int i, j; | |
207 | lang_input_section_type **orig_is; | |
208 | asection **save_s; | |
209 | ||
210 | for (i = 0; i < os_info->count; i = j) | |
211 | { | |
212 | /* Normally a linker script will select SHF_LINK_ORDER sections | |
213 | with an input section wildcard something like the following: | |
214 | *(.IA_64.unwind* .gnu.linkonce.ia64unw.*) | |
215 | However if some other random sections are smashed into an | |
216 | output section, or if SHF_LINK_ORDER are split up by the | |
217 | linker script, then we only want to sort sections matching a | |
218 | given wildcard. That's the purpose of the pattern test. */ | |
219 | for (j = i + 1; j < os_info->count; j++) | |
220 | if (os_info->isec[j].is->pattern != os_info->isec[i].is->pattern) | |
221 | break; | |
222 | if (j - i > 1) | |
223 | qsort (&os_info->isec[i], j - i, sizeof (*os_info->isec), | |
224 | compare_link_order); | |
225 | } | |
226 | for (i = 0; i < os_info->count; i++) | |
227 | if (os_info->isec[i].idx != i) | |
228 | break; | |
229 | if (i == os_info->count) | |
f38a2680 | 230 | return false; |
b209b5a6 AM |
231 | |
232 | /* Now reorder the linker input section statements to reflect the | |
233 | proper sorting. The is done by rewriting the existing statements | |
234 | rather than fiddling with lists, since the only thing we need to | |
235 | change is the bfd section pointer. */ | |
236 | orig_is = xmalloc (os_info->count * sizeof (*orig_is)); | |
237 | save_s = xmalloc (os_info->count * sizeof (*save_s)); | |
238 | for (i = 0; i < os_info->count; i++) | |
239 | { | |
240 | orig_is[os_info->isec[i].idx] = os_info->isec[i].is; | |
241 | save_s[i] = os_info->isec[i].is->section; | |
242 | } | |
243 | for (i = 0; i < os_info->count; i++) | |
244 | if (os_info->isec[i].idx != i) | |
245 | { | |
246 | orig_is[i]->section = save_s[i]; | |
247 | /* Restore os_info to pristine state before the qsort, for the | |
248 | next pass over sections. */ | |
249 | os_info->isec[i].is = orig_is[i]; | |
250 | os_info->isec[i].idx = i; | |
251 | } | |
252 | free (save_s); | |
253 | free (orig_is); | |
f38a2680 | 254 | return true; |
b209b5a6 AM |
255 | } |
256 | ||
d871d478 | 257 | void |
f38a2680 | 258 | ldelf_map_segments (bool need_layout) |
d871d478 AM |
259 | { |
260 | int tries = 10; | |
f38a2680 | 261 | static bool done_link_order_scan = false; |
d871d478 AM |
262 | |
263 | do | |
264 | { | |
265 | lang_relax_sections (need_layout); | |
f38a2680 | 266 | need_layout = false; |
d871d478 | 267 | |
4120e488 | 268 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour) |
b209b5a6 AM |
269 | { |
270 | lang_output_section_statement_type *os; | |
271 | if (!done_link_order_scan) | |
272 | { | |
273 | link_order_scan (statement_list.head, NULL); | |
f38a2680 | 274 | done_link_order_scan = true; |
b209b5a6 AM |
275 | } |
276 | for (os = (void *) lang_os_list.head; os != NULL; os = os->next) | |
277 | { | |
278 | struct os_sections *os_info = os->data; | |
279 | if (os_info != NULL && os_info->ordered != 0) | |
280 | { | |
281 | if (os_info->ordered != os_info->count | |
282 | && bfd_link_relocatable (&link_info)) | |
283 | { | |
284 | einfo (_("%F%P: " | |
ea93a607 | 285 | "%pA has both ordered and unordered sections\n"), |
b209b5a6 AM |
286 | os->bfd_section); |
287 | return; | |
288 | } | |
289 | if (os_info->count > 1 | |
290 | && fixup_link_order (os)) | |
f38a2680 | 291 | need_layout = true; |
b209b5a6 AM |
292 | } |
293 | } | |
294 | } | |
295 | ||
4120e488 | 296 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour |
d871d478 AM |
297 | && !bfd_link_relocatable (&link_info)) |
298 | { | |
299 | bfd_size_type phdr_size; | |
300 | ||
301 | phdr_size = elf_program_header_size (link_info.output_bfd); | |
302 | /* If we don't have user supplied phdrs, throw away any | |
303 | previous linker generated program headers. */ | |
304 | if (lang_phdr_list == NULL) | |
305 | elf_seg_map (link_info.output_bfd) = NULL; | |
306 | if (!_bfd_elf_map_sections_to_segments (link_info.output_bfd, | |
e2cbf4df L |
307 | &link_info, |
308 | &need_layout)) | |
d871d478 AM |
309 | einfo (_("%F%P: map sections to segments failed: %E\n")); |
310 | ||
311 | if (phdr_size != elf_program_header_size (link_info.output_bfd)) | |
312 | { | |
313 | if (tries > 6) | |
314 | /* The first few times we allow any change to | |
315 | phdr_size . */ | |
f38a2680 | 316 | need_layout = true; |
d871d478 AM |
317 | else if (phdr_size |
318 | < elf_program_header_size (link_info.output_bfd)) | |
319 | /* After that we only allow the size to grow. */ | |
f38a2680 | 320 | need_layout = true; |
d871d478 AM |
321 | else |
322 | elf_program_header_size (link_info.output_bfd) = phdr_size; | |
323 | } | |
324 | } | |
325 | } | |
326 | while (need_layout && --tries); | |
327 | ||
328 | if (tries == 0) | |
ea93a607 | 329 | einfo (_("%F%P: looping in map_segments\n")); |
6f6fd151 | 330 | |
4120e488 | 331 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour |
6f6fd151 L |
332 | && lang_phdr_list == NULL) |
333 | { | |
334 | /* If we don't have user supplied phdrs, strip zero-sized dynamic | |
335 | sections and regenerate program headers. */ | |
336 | const struct elf_backend_data *bed | |
337 | = get_elf_backend_data (link_info.output_bfd); | |
338 | if (bed->elf_backend_strip_zero_sized_dynamic_sections | |
339 | && !bed->elf_backend_strip_zero_sized_dynamic_sections | |
340 | (&link_info)) | |
ea93a607 | 341 | einfo (_("%F%P: failed to strip zero-sized dynamic sections\n")); |
6f6fd151 | 342 | } |
d871d478 | 343 | } |
1ff6de03 | 344 | |
094e34f2 | 345 | #ifdef ENABLE_LIBCTF |
1ff6de03 NA |
346 | /* We want to emit CTF early if and only if we are not targetting ELF with this |
347 | invocation. */ | |
348 | ||
349 | int | |
350 | ldelf_emit_ctf_early (void) | |
351 | { | |
352 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour) | |
353 | return 0; | |
354 | return 1; | |
355 | } | |
356 | ||
357 | /* Callbacks used to map from bfd types to libctf types, under libctf's | |
358 | control. */ | |
359 | ||
3d16b64e | 360 | struct ctf_strtab_iter_cb_arg |
1ff6de03 | 361 | { |
3d16b64e | 362 | struct elf_strtab_hash *strtab; |
1ff6de03 NA |
363 | size_t next_i; |
364 | size_t next_idx; | |
365 | }; | |
366 | ||
367 | /* Return strings from the strtab to libctf, one by one. Returns NULL when | |
368 | iteration is complete. */ | |
369 | ||
370 | static const char * | |
371 | ldelf_ctf_strtab_iter_cb (uint32_t *offset, void *arg_) | |
372 | { | |
373 | bfd_size_type off; | |
374 | const char *ret; | |
375 | ||
3d16b64e NA |
376 | struct ctf_strtab_iter_cb_arg *arg = |
377 | (struct ctf_strtab_iter_cb_arg *) arg_; | |
1ff6de03 NA |
378 | |
379 | /* There is no zeroth string. */ | |
380 | if (arg->next_i == 0) | |
381 | arg->next_i = 1; | |
382 | ||
211bcd01 NA |
383 | /* Hunt through strings until we fall off the end or find one with |
384 | a nonzero refcount. */ | |
385 | do | |
1ff6de03 | 386 | { |
211bcd01 NA |
387 | if (arg->next_i >= _bfd_elf_strtab_len (arg->strtab)) |
388 | { | |
389 | arg->next_i = 0; | |
390 | return NULL; | |
391 | } | |
392 | ||
393 | ret = _bfd_elf_strtab_str (arg->strtab, arg->next_i++, &off); | |
1ff6de03 | 394 | } |
211bcd01 | 395 | while (ret == NULL); |
1ff6de03 | 396 | |
1ff6de03 NA |
397 | *offset = off; |
398 | ||
399 | /* If we've overflowed, we cannot share any further strings: the CTF | |
400 | format cannot encode strings with such high offsets. */ | |
401 | if (*offset != off) | |
402 | return NULL; | |
403 | ||
404 | return ret; | |
405 | } | |
406 | ||
3d16b64e NA |
407 | void |
408 | ldelf_acquire_strings_for_ctf | |
409 | (struct ctf_dict *ctf_output, struct elf_strtab_hash *strtab) | |
1ff6de03 | 410 | { |
3d16b64e NA |
411 | struct ctf_strtab_iter_cb_arg args = { strtab, 0, 0 }; |
412 | if (!ctf_output) | |
413 | return; | |
1ff6de03 | 414 | |
3d16b64e | 415 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour) |
1ff6de03 | 416 | { |
3d16b64e NA |
417 | if (ctf_link_add_strtab (ctf_output, ldelf_ctf_strtab_iter_cb, |
418 | &args) < 0) | |
419 | einfo (_("%F%P: warning: CTF strtab association failed; strings will " | |
420 | "not be shared: %s\n"), | |
421 | ctf_errmsg (ctf_errno (ctf_output))); | |
1ff6de03 | 422 | } |
1ff6de03 NA |
423 | } |
424 | ||
425 | void | |
3d16b64e NA |
426 | ldelf_new_dynsym_for_ctf (struct ctf_dict *ctf_output, int symidx, |
427 | struct elf_internal_sym *sym) | |
1ff6de03 | 428 | { |
3d16b64e NA |
429 | ctf_link_sym_t lsym; |
430 | ||
431 | if (!ctf_output) | |
1ff6de03 NA |
432 | return; |
433 | ||
3d16b64e NA |
434 | /* New symbol. */ |
435 | if (sym != NULL) | |
1ff6de03 | 436 | { |
3d16b64e NA |
437 | lsym.st_name = NULL; |
438 | lsym.st_nameidx = sym->st_name; | |
439 | lsym.st_nameidx_set = 1; | |
440 | lsym.st_symidx = symidx; | |
441 | lsym.st_shndx = sym->st_shndx; | |
442 | lsym.st_type = ELF_ST_TYPE (sym->st_info); | |
443 | lsym.st_value = sym->st_value; | |
444 | if (ctf_link_add_linker_symbol (ctf_output, &lsym) < 0) | |
445 | { | |
446 | einfo (_("%F%P: warning: CTF symbol addition failed; CTF will " | |
447 | "not be tied to symbols: %s\n"), | |
448 | ctf_errmsg (ctf_errno (ctf_output))); | |
449 | } | |
450 | } | |
451 | else | |
452 | { | |
453 | /* Shuffle all the symbols. */ | |
1ff6de03 | 454 | |
3d16b64e NA |
455 | if (ctf_link_shuffle_syms (ctf_output) < 0) |
456 | einfo (_("%F%P: warning: CTF symbol shuffling failed; CTF will " | |
457 | "not be tied to symbols: %s\n"), | |
458 | ctf_errmsg (ctf_errno (ctf_output))); | |
1ff6de03 NA |
459 | } |
460 | } | |
094e34f2 | 461 | #else |
3d16b64e NA |
462 | int |
463 | ldelf_emit_ctf_early (void) | |
094e34f2 NA |
464 | { |
465 | return 0; | |
466 | } | |
467 | ||
3d16b64e NA |
468 | void |
469 | ldelf_acquire_strings_for_ctf (struct ctf_dict *ctf_output ATTRIBUTE_UNUSED, | |
470 | struct elf_strtab_hash *strtab ATTRIBUTE_UNUSED) | |
471 | {} | |
472 | void | |
473 | ldelf_new_dynsym_for_ctf (struct ctf_dict *ctf_output ATTRIBUTE_UNUSED, | |
474 | int symidx ATTRIBUTE_UNUSED, | |
475 | struct elf_internal_sym *sym ATTRIBUTE_UNUSED) | |
094e34f2 NA |
476 | {} |
477 | #endif |