Commit | Line | Data |
---|---|---|
1f808cd5 | 1 | # This shell script emits a C file. -*- C -*- |
2571583a | 2 | # Copyright (C) 2002-2017 Free Software Foundation, Inc. |
1f808cd5 | 3 | # |
f96b4a7b | 4 | # This file is part of the GNU Binutils. |
1f808cd5 AM |
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 | |
f96b4a7b | 8 | # the Free Software Foundation; either version 3 of the License, or |
1f808cd5 AM |
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 | |
f96b4a7b NC |
18 | # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
19 | # MA 02110-1301, USA. | |
1f808cd5 AM |
20 | # |
21 | ||
22 | # This file is sourced from elf32.em, and defines extra powerpc64-elf | |
23 | # specific routines. | |
24 | # | |
92b93329 | 25 | fragment <<EOF |
1f808cd5 | 26 | |
9c1d81c1 | 27 | #include "ldctor.h" |
83490352 | 28 | #include "elf-bfd.h" |
1f808cd5 | 29 | #include "elf64-ppc.h" |
f05eb3b7 | 30 | #include "ldlex.h" |
7341d5e2 | 31 | #include "elf/ppc64.h" |
1f808cd5 | 32 | |
e7d1c40c AM |
33 | static asection *ppc_add_stub_section (const char *, asection *); |
34 | static void ppc_layout_sections_again (void); | |
35 | ||
36 | static struct ppc64_elf_params params = { NULL, | |
37 | &ppc_add_stub_section, | |
38 | &ppc_layout_sections_again, | |
7c9cf415 | 39 | 1, -1, 0, |
e7d1c40c | 40 | ${DEFAULT_PLT_STATIC_CHAIN-0}, -1, 0, |
dbd1e97e | 41 | 0, -1, -1, 0}; |
e7d1c40c | 42 | |
9c1d81c1 AM |
43 | /* Fake input file for stubs. */ |
44 | static lang_input_statement_type *stub_file; | |
45 | ||
deb04cdb AM |
46 | /* Whether we need to call ppc_layout_sections_again. */ |
47 | static int need_laying_out = 0; | |
48 | ||
fac1652d AM |
49 | /* Whether to add ".foo" entries for each "foo" in a version script. */ |
50 | static int dotsyms = 1; | |
51 | ||
e5096e3f | 52 | /* Whether to run tls optimization. */ |
c5614fa4 AM |
53 | static int no_tls_opt = 0; |
54 | ||
55 | /* Whether to run opd optimization. */ | |
56 | static int no_opd_opt = 0; | |
57 | ||
58 | /* Whether to run toc optimization. */ | |
59 | static int no_toc_opt = 0; | |
e5096e3f | 60 | |
d43d0b53 AM |
61 | /* Whether to sort input toc and got sections. */ |
62 | static int no_toc_sort = 0; | |
63 | ||
6c2573b7 AM |
64 | /* Input .toc sections will be placed in this output section. */ |
65 | static const char *toc_section_name = ".got"; | |
0b9a4d73 AM |
66 | static asection *toc_section = 0; |
67 | ||
9c1d81c1 AM |
68 | /* This is called before the input files are opened. We create a new |
69 | fake input file to hold the stub sections. */ | |
70 | ||
71 | static void | |
7d8a3a28 | 72 | ppc_create_output_section_statements (void) |
9c1d81c1 | 73 | { |
5503fea1 | 74 | if (!(bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour |
4dfe6ac6 | 75 | && elf_object_id (link_info.output_bfd) == PPC64_ELF_DATA)) |
a015f5ec AM |
76 | return; |
77 | ||
b9cf773d AM |
78 | link_info.wrap_char = '.'; |
79 | ||
9c1d81c1 AM |
80 | stub_file = lang_add_input_file ("linker stubs", |
81 | lang_input_file_is_fake_enum, | |
82 | NULL); | |
f13a99db | 83 | stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd); |
9c1d81c1 AM |
84 | if (stub_file->the_bfd == NULL |
85 | || !bfd_set_arch_mach (stub_file->the_bfd, | |
f13a99db AM |
86 | bfd_get_arch (link_info.output_bfd), |
87 | bfd_get_mach (link_info.output_bfd))) | |
9c1d81c1 | 88 | { |
f7c46796 | 89 | einfo ("%F%P: can not create BFD: %E\n"); |
9c1d81c1 AM |
90 | return; |
91 | } | |
92 | ||
d457dcf6 | 93 | stub_file->the_bfd->flags |= BFD_LINKER_CREATED; |
9c1d81c1 | 94 | ldlang_add_file (stub_file); |
e7d1c40c | 95 | params.stub_bfd = stub_file->the_bfd; |
7d4c687d | 96 | if (params.save_restore_funcs < 0) |
0e1862bb | 97 | params.save_restore_funcs = !bfd_link_relocatable (&link_info); |
e7d1c40c | 98 | if (!ppc64_elf_init_stub_bfd (&link_info, ¶ms)) |
bfeb4a28 | 99 | einfo ("%F%P: can not init BFD: %E\n"); |
9c1d81c1 AM |
100 | } |
101 | ||
dbd1e97e AM |
102 | /* Called after opening files but before mapping sections. */ |
103 | ||
104 | static void | |
105 | ppc_after_open (void) | |
106 | { | |
107 | if (stub_file != NULL && link_info.relro && params.object_in_toc) | |
108 | { | |
109 | /* We have a .toc section that might be written to at run time. | |
110 | Don't put .toc into the .got output section. */ | |
111 | lang_output_section_statement_type *got; | |
112 | ||
113 | got = lang_output_section_find (".got"); | |
114 | if (got != NULL) | |
115 | { | |
116 | lang_statement_union_type *s; | |
117 | for (s = got->children.head; s != NULL; s = s->header.next) | |
118 | if (s->header.type == lang_wild_statement_enum | |
119 | && s->wild_statement.filename == NULL) | |
120 | { | |
121 | struct wildcard_list **i = &s->wild_statement.section_list; | |
122 | while (*i != NULL) | |
123 | if (strcmp ((*i)->spec.name, ".toc") == 0) | |
124 | *i = (*i)->next; | |
125 | else | |
126 | i = &(*i)->next; | |
127 | } | |
6c2573b7 AM |
128 | /* Instead, .toc input sections will be mapped to the |
129 | read/write .toc output section. If user scripts don't | |
130 | provide one then we'll lose toc sorting and multi-toc. */ | |
131 | toc_section_name = ".toc"; | |
dbd1e97e AM |
132 | } |
133 | } | |
134 | gld${EMULATION_NAME}_after_open (); | |
135 | } | |
136 | ||
d43d0b53 AM |
137 | /* Move the input section statement at *U which happens to be on LIST |
138 | to be just before *TO. */ | |
139 | ||
140 | static void | |
141 | move_input_section (lang_statement_list_type *list, | |
142 | lang_statement_union_type **u, | |
143 | lang_statement_union_type **to) | |
144 | { | |
145 | lang_statement_union_type *s = *u; | |
146 | asection *i = s->input_section.section; | |
147 | asection *p, *n; | |
148 | ||
149 | /* Snip the input section from the statement list. If it was the | |
150 | last statement, fix the list tail pointer. */ | |
151 | *u = s->header.next; | |
152 | if (*u == NULL) | |
153 | list->tail = u; | |
154 | /* Add it back in the new position. */ | |
155 | s->header.next = *to; | |
156 | *to = s; | |
157 | if (list->tail == to) | |
158 | list->tail = &s->header.next; | |
159 | ||
160 | /* Trim I off the bfd map_head/map_tail doubly linked lists. */ | |
161 | n = i->map_head.s; | |
162 | p = i->map_tail.s; | |
163 | (p != NULL ? p : i->output_section)->map_head.s = n; | |
164 | (n != NULL ? n : i->output_section)->map_tail.s = p; | |
165 | ||
166 | /* Add I back on in its new position. */ | |
167 | if (s->header.next->header.type == lang_input_section_enum) | |
168 | { | |
169 | n = s->header.next->input_section.section; | |
170 | p = n->map_tail.s; | |
171 | } | |
172 | else | |
173 | { | |
174 | /* If the next statement is not an input section statement then | |
175 | TO must point at the previous input section statement | |
176 | header.next field. */ | |
177 | lang_input_section_type *prev = (lang_input_section_type *) | |
178 | ((char *) to - offsetof (lang_statement_union_type, header.next)); | |
179 | ||
180 | ASSERT (prev->header.type == lang_input_section_enum); | |
181 | p = prev->section; | |
182 | n = p->map_head.s; | |
183 | } | |
184 | i->map_head.s = n; | |
185 | i->map_tail.s = p; | |
186 | (p != NULL ? p : i->output_section)->map_head.s = i; | |
187 | (n != NULL ? n : i->output_section)->map_tail.s = i; | |
188 | } | |
189 | ||
190 | /* Sort input section statements in the linker script tree rooted at | |
191 | LIST so that those whose owning bfd happens to have a section | |
192 | called .init or .fini are placed first. Place any TOC sections | |
193 | referenced by small TOC relocs next, with TOC sections referenced | |
194 | only by bigtoc relocs last. */ | |
195 | ||
196 | static void | |
197 | sort_toc_sections (lang_statement_list_type *list, | |
198 | lang_statement_union_type **ini, | |
199 | lang_statement_union_type **small) | |
200 | { | |
201 | lang_statement_union_type *s, **u; | |
202 | asection *i; | |
203 | ||
204 | u = &list->head; | |
205 | while ((s = *u) != NULL) | |
206 | { | |
207 | switch (s->header.type) | |
208 | { | |
209 | case lang_wild_statement_enum: | |
210 | sort_toc_sections (&s->wild_statement.children, ini, small); | |
211 | break; | |
212 | ||
213 | case lang_group_statement_enum: | |
214 | sort_toc_sections (&s->group_statement.children, ini, small); | |
215 | break; | |
216 | ||
217 | case lang_input_section_enum: | |
218 | i = s->input_section.section; | |
219 | /* Leave the stub_file .got where it is. We put the .got | |
220 | header there. */ | |
221 | if (i->owner == stub_file->the_bfd) | |
222 | break; | |
223 | if (bfd_get_section_by_name (i->owner, ".init") != NULL | |
224 | || bfd_get_section_by_name (i->owner, ".fini") != NULL) | |
225 | { | |
226 | if (ini != NULL && *ini != s) | |
227 | { | |
228 | move_input_section (list, u, ini); | |
229 | if (small == ini) | |
230 | small = &s->header.next; | |
231 | ini = &s->header.next; | |
232 | continue; | |
233 | } | |
234 | if (small == ini) | |
235 | small = &s->header.next; | |
236 | ini = &s->header.next; | |
237 | break; | |
238 | } | |
239 | else if (ini == NULL) | |
240 | ini = u; | |
241 | ||
242 | if (ppc64_elf_has_small_toc_reloc (i)) | |
243 | { | |
244 | if (small != NULL && *small != s) | |
245 | { | |
246 | move_input_section (list, u, small); | |
247 | small = &s->header.next; | |
248 | continue; | |
249 | } | |
250 | small = &s->header.next; | |
251 | } | |
252 | else if (small == NULL) | |
253 | small = u; | |
254 | break; | |
255 | ||
256 | default: | |
257 | break; | |
258 | } | |
259 | u = &s->header.next; | |
260 | } | |
261 | } | |
262 | ||
ba761f19 AM |
263 | static void |
264 | prelim_size_sections (void) | |
265 | { | |
266 | if (expld.phase != lang_mark_phase_enum) | |
267 | { | |
268 | expld.phase = lang_mark_phase_enum; | |
269 | expld.dataseg.phase = exp_dataseg_none; | |
270 | one_lang_size_sections_pass (NULL, FALSE); | |
271 | /* We must not cache anything from the preliminary sizing. */ | |
272 | lang_reset_memory_regions (); | |
273 | } | |
274 | } | |
275 | ||
836c6af1 | 276 | static void |
7d8a3a28 | 277 | ppc_before_allocation (void) |
836c6af1 | 278 | { |
a015f5ec | 279 | if (stub_file != NULL) |
e0468e59 | 280 | { |
74f0fb50 | 281 | if (!no_opd_opt |
e7d1c40c | 282 | && !ppc64_elf_edit_opd (&link_info)) |
f7c46796 | 283 | einfo ("%X%P: can not edit %s: %E\n", "opd"); |
e5096e3f | 284 | |
e7d1c40c | 285 | if (ppc64_elf_tls_setup (&link_info) |
f13a99db | 286 | && !no_tls_opt) |
a015f5ec AM |
287 | { |
288 | /* Size the sections. This is premature, but we want to know the | |
289 | TLS segment layout so that certain optimizations can be done. */ | |
ba761f19 | 290 | prelim_size_sections (); |
a015f5ec | 291 | |
33c0ec9d | 292 | if (!ppc64_elf_tls_optimize (&link_info)) |
92b7a70f | 293 | einfo ("%X%P: TLS problem %E\n"); |
a015f5ec | 294 | } |
c5614fa4 AM |
295 | |
296 | if (!no_toc_opt | |
0e1862bb | 297 | && !bfd_link_relocatable (&link_info)) |
ba761f19 AM |
298 | { |
299 | prelim_size_sections (); | |
300 | ||
301 | if (!ppc64_elf_edit_toc (&link_info)) | |
f7c46796 | 302 | einfo ("%X%P: can not edit %s: %E\n", "toc"); |
ba761f19 | 303 | } |
d43d0b53 AM |
304 | |
305 | if (!no_toc_sort) | |
306 | { | |
307 | lang_output_section_statement_type *toc_os; | |
308 | ||
6c2573b7 | 309 | toc_os = lang_output_section_find (toc_section_name); |
d43d0b53 AM |
310 | if (toc_os != NULL) |
311 | sort_toc_sections (&toc_os->children, NULL, NULL); | |
312 | } | |
e0468e59 AM |
313 | } |
314 | ||
836c6af1 AM |
315 | gld${EMULATION_NAME}_before_allocation (); |
316 | } | |
317 | ||
9c1d81c1 AM |
318 | struct hook_stub_info |
319 | { | |
320 | lang_statement_list_type add; | |
321 | asection *input_section; | |
322 | }; | |
323 | ||
324 | /* Traverse the linker tree to find the spot where the stub goes. */ | |
325 | ||
b34976b6 | 326 | static bfd_boolean |
7d8a3a28 | 327 | hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp) |
9c1d81c1 AM |
328 | { |
329 | lang_statement_union_type *l; | |
b34976b6 | 330 | bfd_boolean ret; |
9c1d81c1 AM |
331 | |
332 | for (; (l = *lp) != NULL; lp = &l->header.next) | |
333 | { | |
334 | switch (l->header.type) | |
335 | { | |
336 | case lang_constructors_statement_enum: | |
337 | ret = hook_in_stub (info, &constructor_list.head); | |
338 | if (ret) | |
339 | return ret; | |
340 | break; | |
341 | ||
342 | case lang_output_section_statement_enum: | |
343 | ret = hook_in_stub (info, | |
344 | &l->output_section_statement.children.head); | |
345 | if (ret) | |
346 | return ret; | |
347 | break; | |
348 | ||
349 | case lang_wild_statement_enum: | |
350 | ret = hook_in_stub (info, &l->wild_statement.children.head); | |
351 | if (ret) | |
352 | return ret; | |
353 | break; | |
354 | ||
355 | case lang_group_statement_enum: | |
356 | ret = hook_in_stub (info, &l->group_statement.children.head); | |
357 | if (ret) | |
358 | return ret; | |
359 | break; | |
360 | ||
361 | case lang_input_section_enum: | |
362 | if (l->input_section.section == info->input_section) | |
363 | { | |
364 | /* We've found our section. Insert the stub immediately | |
365 | before its associated input section. */ | |
366 | *lp = info->add.head; | |
367 | *(info->add.tail) = l; | |
b34976b6 | 368 | return TRUE; |
9c1d81c1 AM |
369 | } |
370 | break; | |
371 | ||
372 | case lang_data_statement_enum: | |
373 | case lang_reloc_statement_enum: | |
374 | case lang_object_symbols_statement_enum: | |
375 | case lang_output_statement_enum: | |
376 | case lang_target_statement_enum: | |
377 | case lang_input_statement_enum: | |
378 | case lang_assignment_statement_enum: | |
379 | case lang_padding_statement_enum: | |
380 | case lang_address_statement_enum: | |
381 | case lang_fill_statement_enum: | |
382 | break; | |
383 | ||
384 | default: | |
385 | FAIL (); | |
386 | break; | |
387 | } | |
388 | } | |
b34976b6 | 389 | return FALSE; |
9c1d81c1 AM |
390 | } |
391 | ||
392 | ||
393 | /* Call-back for ppc64_elf_size_stubs. */ | |
394 | ||
395 | /* Create a new stub section, and arrange for it to be linked | |
396 | immediately before INPUT_SECTION. */ | |
397 | ||
398 | static asection * | |
7d8a3a28 | 399 | ppc_add_stub_section (const char *stub_sec_name, asection *input_section) |
9c1d81c1 AM |
400 | { |
401 | asection *stub_sec; | |
402 | flagword flags; | |
403 | asection *output_section; | |
9c1d81c1 AM |
404 | lang_output_section_statement_type *os; |
405 | struct hook_stub_info info; | |
406 | ||
9c1d81c1 | 407 | flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE |
e4e0193e | 408 | | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_KEEP); |
9795b468 AM |
409 | stub_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd, |
410 | stub_sec_name, flags); | |
bd518033 | 411 | if (stub_sec == NULL |
794e51c0 | 412 | || !bfd_set_section_alignment (stub_file->the_bfd, stub_sec, |
e05fa0ba AM |
413 | (params.plt_stub_align > 5 |
414 | ? params.plt_stub_align | |
415 | : 5))) | |
9c1d81c1 AM |
416 | goto err_ret; |
417 | ||
418 | output_section = input_section->output_section; | |
24ef1aa7 | 419 | os = lang_output_section_get (output_section); |
9c1d81c1 AM |
420 | |
421 | info.input_section = input_section; | |
422 | lang_list_init (&info.add); | |
b9c361e0 | 423 | lang_add_section (&info.add, stub_sec, NULL, os); |
9c1d81c1 AM |
424 | |
425 | if (info.add.head == NULL) | |
426 | goto err_ret; | |
427 | ||
428 | if (hook_in_stub (&info, &os->children.head)) | |
429 | return stub_sec; | |
430 | ||
431 | err_ret: | |
432 | einfo ("%X%P: can not make stub section: %E\n"); | |
433 | return NULL; | |
434 | } | |
435 | ||
436 | ||
437 | /* Another call-back for ppc64_elf_size_stubs. */ | |
438 | ||
439 | static void | |
7d8a3a28 | 440 | ppc_layout_sections_again (void) |
9c1d81c1 AM |
441 | { |
442 | /* If we have changed sizes of the stub sections, then we need | |
443 | to recalculate all the section offsets. This may mean we need to | |
444 | add even more stubs. */ | |
8ded5a0f | 445 | gld${EMULATION_NAME}_map_segments (TRUE); |
1f808cd5 | 446 | |
0e1862bb | 447 | if (!bfd_link_relocatable (&link_info)) |
1c865ab2 | 448 | ppc64_elf_set_toc (&link_info, link_info.output_bfd); |
eaeb0a9d AM |
449 | |
450 | need_laying_out = -1; | |
1f808cd5 AM |
451 | } |
452 | ||
9c1d81c1 | 453 | |
0b9a4d73 | 454 | static void |
7d8a3a28 | 455 | build_toc_list (lang_statement_union_type *statement) |
0b9a4d73 | 456 | { |
7b986e99 AM |
457 | if (statement->header.type == lang_input_section_enum) |
458 | { | |
459 | asection *i = statement->input_section.section; | |
460 | ||
dbaa2011 | 461 | if (i->sec_info_type != SEC_INFO_TYPE_JUST_SYMS |
7b986e99 AM |
462 | && (i->flags & SEC_EXCLUDE) == 0 |
463 | && i->output_section == toc_section) | |
927be08e AM |
464 | { |
465 | if (!ppc64_elf_next_toc_section (&link_info, i)) | |
466 | einfo ("%X%P: linker script separates .got and .toc\n"); | |
467 | } | |
7b986e99 | 468 | } |
0b9a4d73 AM |
469 | } |
470 | ||
471 | ||
9c1d81c1 | 472 | static void |
7d8a3a28 | 473 | build_section_lists (lang_statement_union_type *statement) |
9c1d81c1 | 474 | { |
7b986e99 | 475 | if (statement->header.type == lang_input_section_enum) |
9c1d81c1 | 476 | { |
7b986e99 AM |
477 | asection *i = statement->input_section.section; |
478 | ||
66be1055 | 479 | if (!((lang_input_statement_type *) i->owner->usrdata)->flags.just_syms |
7b986e99 AM |
480 | && (i->flags & SEC_EXCLUDE) == 0 |
481 | && i->output_section != NULL | |
f13a99db | 482 | && i->output_section->owner == link_info.output_bfd) |
7b986e99 AM |
483 | { |
484 | if (!ppc64_elf_next_input_section (&link_info, i)) | |
485 | einfo ("%X%P: can not size stub section: %E\n"); | |
486 | } | |
9c1d81c1 AM |
487 | } |
488 | } | |
489 | ||
fac1652d | 490 | |
eaeb0a9d AM |
491 | /* Call the back-end function to set TOC base after we have placed all |
492 | the sections. */ | |
1f808cd5 | 493 | static void |
eaeb0a9d | 494 | gld${EMULATION_NAME}_after_allocation (void) |
1f808cd5 | 495 | { |
75938853 AM |
496 | int ret; |
497 | ||
836c6af1 AM |
498 | /* If generating a relocatable output file, then we don't have any |
499 | stubs. */ | |
0e1862bb | 500 | if (stub_file != NULL && !bfd_link_relocatable (&link_info)) |
1f808cd5 | 501 | { |
75938853 | 502 | ret = ppc64_elf_setup_section_lists (&link_info); |
92b7a70f AM |
503 | if (ret < 0) |
504 | einfo ("%X%P: can not size stub section: %E\n"); | |
da44f4e5 | 505 | else |
9c1d81c1 | 506 | { |
927be08e AM |
507 | ppc64_elf_start_multitoc_partition (&link_info); |
508 | ||
e7d1c40c | 509 | if (!params.no_multi_toc) |
927be08e AM |
510 | { |
511 | toc_section = bfd_get_section_by_name (link_info.output_bfd, | |
6c2573b7 | 512 | toc_section_name); |
927be08e AM |
513 | if (toc_section != NULL) |
514 | lang_for_each_statement (build_toc_list); | |
515 | } | |
516 | ||
517 | if (ppc64_elf_layout_multitoc (&link_info) | |
e7d1c40c | 518 | && !params.no_multi_toc |
927be08e | 519 | && toc_section != NULL) |
0b9a4d73 AM |
520 | lang_for_each_statement (build_toc_list); |
521 | ||
927be08e | 522 | ppc64_elf_finish_multitoc_partition (&link_info); |
0b9a4d73 | 523 | |
836c6af1 | 524 | lang_for_each_statement (build_section_lists); |
9c1d81c1 | 525 | |
70cc837d AM |
526 | if (!ppc64_elf_check_init_fini (&link_info)) |
527 | einfo ("%P: .init/.fini fragments use differing TOC pointers\n"); | |
528 | ||
836c6af1 | 529 | /* Call into the BFD backend to do the real work. */ |
e7d1c40c | 530 | if (!ppc64_elf_size_stubs (&link_info)) |
92b7a70f | 531 | einfo ("%X%P: can not size stub section: %E\n"); |
1f808cd5 | 532 | } |
9c1d81c1 | 533 | } |
1f808cd5 | 534 | |
da44f4e5 AM |
535 | /* We can't parse and merge .eh_frame until the glink .eh_frame has |
536 | been generated. Otherwise the glink .eh_frame CIE won't be | |
537 | merged with other CIEs, and worse, the glink .eh_frame FDEs won't | |
538 | be listed in .eh_frame_hdr. */ | |
539 | ret = bfd_elf_discard_info (link_info.output_bfd, &link_info); | |
540 | if (ret < 0) | |
541 | { | |
542 | einfo ("%X%P: .eh_frame/.stab edit: %E\n"); | |
543 | return; | |
544 | } | |
545 | else if (ret > 0) | |
546 | need_laying_out = 1; | |
547 | ||
44bd1acd AM |
548 | /* Call map_segments regardless of the state of need_laying_out. |
549 | need_laying_out set to -1 means we have just laid everything out, | |
550 | but ppc64_elf_size_stubs strips .branch_lt and .eh_frame if | |
551 | unneeded, after ppc_layout_sections_again. Another call removes | |
552 | these sections from the segment map. Their presence is | |
553 | innocuous except for confusing ELF_SECTION_IN_SEGMENT. */ | |
554 | gld${EMULATION_NAME}_map_segments (need_laying_out > 0); | |
555 | ||
0e1862bb | 556 | if (need_laying_out != -1 && !bfd_link_relocatable (&link_info)) |
44bd1acd | 557 | ppc64_elf_set_toc (&link_info, link_info.output_bfd); |
eaeb0a9d AM |
558 | } |
559 | ||
560 | ||
561 | /* Final emulation specific call. */ | |
562 | ||
563 | static void | |
564 | gld${EMULATION_NAME}_finish (void) | |
565 | { | |
7341d5e2 AM |
566 | char *msg = NULL; |
567 | char *line, *endline; | |
568 | ||
eaeb0a9d AM |
569 | /* e_entry on PowerPC64 points to the function descriptor for |
570 | _start. If _start is missing, default to the first function | |
571 | descriptor in the .opd section. */ | |
459609d6 AM |
572 | if (stub_file != NULL |
573 | && (elf_elfheader (link_info.output_bfd)->e_flags & EF_PPC64_ABI) == 1) | |
7341d5e2 AM |
574 | entry_section = ".opd"; |
575 | ||
576 | if (params.emit_stub_syms < 0) | |
577 | params.emit_stub_syms = 1; | |
578 | if (stub_file != NULL | |
0e1862bb | 579 | && !bfd_link_relocatable (&link_info) |
7341d5e2 AM |
580 | && !ppc64_elf_build_stubs (&link_info, config.stats ? &msg : NULL)) |
581 | einfo ("%X%P: can not build stubs: %E\n"); | |
582 | ||
583 | fflush (stdout); | |
584 | for (line = msg; line != NULL; line = endline) | |
9c1d81c1 | 585 | { |
7341d5e2 AM |
586 | endline = strchr (line, '\n'); |
587 | if (endline != NULL) | |
588 | *endline++ = '\0'; | |
589 | fprintf (stderr, "%s: %s\n", program_name, line); | |
9c1d81c1 | 590 | } |
7341d5e2 AM |
591 | fflush (stderr); |
592 | if (msg != NULL) | |
593 | free (msg); | |
99877b66 | 594 | |
1e035701 | 595 | finish_default (); |
9c1d81c1 | 596 | } |
1f808cd5 | 597 | |
76dc39fe | 598 | |
fac1652d AM |
599 | /* Add a pattern matching ".foo" for every "foo" in a version script. |
600 | ||
601 | The reason for doing this is that many shared library version | |
602 | scripts export a selected set of functions or data symbols, forcing | |
603 | others local. eg. | |
604 | ||
605 | . VERS_1 { | |
606 | . global: | |
607 | . this; that; some; thing; | |
608 | . local: | |
609 | . *; | |
610 | . }; | |
611 | ||
612 | To make the above work for PowerPC64, we need to export ".this", | |
613 | ".that" and so on, otherwise only the function descriptor syms are | |
614 | exported. Lack of an exported function code sym may cause a | |
615 | definition to be pulled in from a static library. */ | |
616 | ||
7d8a3a28 AM |
617 | static struct bfd_elf_version_expr * |
618 | gld${EMULATION_NAME}_new_vers_pattern (struct bfd_elf_version_expr *entry) | |
fac1652d AM |
619 | { |
620 | struct bfd_elf_version_expr *dot_entry; | |
fac1652d AM |
621 | unsigned int len; |
622 | char *dot_pat; | |
623 | ||
b7b7fe3f | 624 | if (!dotsyms |
ae5a3597 AM |
625 | || entry->pattern[0] == '.' |
626 | || (!entry->literal && entry->pattern[0] == '*')) | |
fac1652d AM |
627 | return entry; |
628 | ||
7d8a3a28 | 629 | dot_entry = xmalloc (sizeof *dot_entry); |
108ba305 | 630 | *dot_entry = *entry; |
fac1652d | 631 | dot_entry->next = entry; |
ae5a3597 AM |
632 | len = strlen (entry->pattern) + 2; |
633 | dot_pat = xmalloc (len); | |
634 | dot_pat[0] = '.'; | |
635 | memcpy (dot_pat + 1, entry->pattern, len - 1); | |
636 | dot_entry->pattern = dot_pat; | |
71755fdf | 637 | dot_entry->script = 1; |
fac1652d AM |
638 | return dot_entry; |
639 | } | |
640 | ||
641 | ||
9c1d81c1 AM |
642 | /* Avoid processing the fake stub_file in vercheck, stat_needed and |
643 | check_needed routines. */ | |
1f808cd5 | 644 | |
7d8a3a28 | 645 | static void (*real_func) (lang_input_statement_type *); |
1f808cd5 | 646 | |
7d8a3a28 | 647 | static void ppc_for_each_input_file_wrapper (lang_input_statement_type *l) |
9c1d81c1 AM |
648 | { |
649 | if (l != stub_file) | |
650 | (*real_func) (l); | |
1f808cd5 | 651 | } |
9c1d81c1 AM |
652 | |
653 | static void | |
7d8a3a28 | 654 | ppc_lang_for_each_input_file (void (*func) (lang_input_statement_type *)) |
9c1d81c1 AM |
655 | { |
656 | real_func = func; | |
657 | lang_for_each_input_file (&ppc_for_each_input_file_wrapper); | |
658 | } | |
659 | ||
660 | #define lang_for_each_input_file ppc_lang_for_each_input_file | |
661 | ||
1f808cd5 AM |
662 | EOF |
663 | ||
dc27aea4 | 664 | if grep -q 'ld_elf32_spu_emulation' ldemul-list.h; then |
92b93329 | 665 | fragment <<EOF |
dc27aea4 AM |
666 | /* Special handling for embedded SPU executables. */ |
667 | extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *); | |
668 | static bfd_boolean gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *); | |
669 | ||
670 | static bfd_boolean | |
671 | ppc64_recognized_file (lang_input_statement_type *entry) | |
672 | { | |
673 | if (embedded_spu_file (entry, "-m64")) | |
674 | return TRUE; | |
675 | ||
676 | return gld${EMULATION_NAME}_load_symbols (entry); | |
677 | } | |
678 | EOF | |
679 | LDEMUL_RECOGNIZED_FILE=ppc64_recognized_file | |
680 | fi | |
681 | ||
9c1d81c1 AM |
682 | # Define some shell vars to insert bits of code into the standard elf |
683 | # parse_args and list_options functions. | |
684 | # | |
58d180e8 AM |
685 | PARSE_AND_LIST_PROLOGUE=${PARSE_AND_LIST_PROLOGUE}' |
686 | #define OPTION_STUBGROUP_SIZE 321 | |
9df0ef5f AM |
687 | #define OPTION_PLT_STATIC_CHAIN (OPTION_STUBGROUP_SIZE + 1) |
688 | #define OPTION_NO_PLT_STATIC_CHAIN (OPTION_PLT_STATIC_CHAIN + 1) | |
794e51c0 AM |
689 | #define OPTION_PLT_THREAD_SAFE (OPTION_NO_PLT_STATIC_CHAIN + 1) |
690 | #define OPTION_NO_PLT_THREAD_SAFE (OPTION_PLT_THREAD_SAFE + 1) | |
691 | #define OPTION_PLT_ALIGN (OPTION_NO_PLT_THREAD_SAFE + 1) | |
692 | #define OPTION_NO_PLT_ALIGN (OPTION_PLT_ALIGN + 1) | |
693 | #define OPTION_STUBSYMS (OPTION_NO_PLT_ALIGN + 1) | |
b02c4cfa | 694 | #define OPTION_NO_STUBSYMS (OPTION_STUBSYMS + 1) |
7d4c687d AM |
695 | #define OPTION_SAVRES (OPTION_NO_STUBSYMS + 1) |
696 | #define OPTION_NO_SAVRES (OPTION_SAVRES + 1) | |
697 | #define OPTION_DOTSYMS (OPTION_NO_SAVRES + 1) | |
fac1652d | 698 | #define OPTION_NO_DOTSYMS (OPTION_DOTSYMS + 1) |
e5096e3f | 699 | #define OPTION_NO_TLS_OPT (OPTION_NO_DOTSYMS + 1) |
7c9cf415 AM |
700 | #define OPTION_TLS_GET_ADDR_OPT (OPTION_NO_TLS_OPT + 1) |
701 | #define OPTION_NO_TLS_GET_ADDR_OPT (OPTION_TLS_GET_ADDR_OPT + 1) | |
a7f2871e | 702 | #define OPTION_NO_OPD_OPT (OPTION_NO_TLS_GET_ADDR_OPT + 1) |
c5614fa4 | 703 | #define OPTION_NO_TOC_OPT (OPTION_NO_OPD_OPT + 1) |
4c52953f | 704 | #define OPTION_NO_MULTI_TOC (OPTION_NO_TOC_OPT + 1) |
d43d0b53 AM |
705 | #define OPTION_NO_TOC_SORT (OPTION_NO_MULTI_TOC + 1) |
706 | #define OPTION_NON_OVERLAPPING_OPD (OPTION_NO_TOC_SORT + 1) | |
9c1d81c1 AM |
707 | ' |
708 | ||
58d180e8 | 709 | PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}' |
9c1d81c1 | 710 | { "stub-group-size", required_argument, NULL, OPTION_STUBGROUP_SIZE }, |
9df0ef5f AM |
711 | { "plt-static-chain", no_argument, NULL, OPTION_PLT_STATIC_CHAIN }, |
712 | { "no-plt-static-chain", no_argument, NULL, OPTION_NO_PLT_STATIC_CHAIN }, | |
794e51c0 AM |
713 | { "plt-thread-safe", no_argument, NULL, OPTION_PLT_THREAD_SAFE }, |
714 | { "no-plt-thread-safe", no_argument, NULL, OPTION_NO_PLT_THREAD_SAFE }, | |
715 | { "plt-align", optional_argument, NULL, OPTION_PLT_ALIGN }, | |
716 | { "no-plt-align", no_argument, NULL, OPTION_NO_PLT_ALIGN }, | |
0b9a4d73 | 717 | { "emit-stub-syms", no_argument, NULL, OPTION_STUBSYMS }, |
b02c4cfa | 718 | { "no-emit-stub-syms", no_argument, NULL, OPTION_NO_STUBSYMS }, |
fac1652d AM |
719 | { "dotsyms", no_argument, NULL, OPTION_DOTSYMS }, |
720 | { "no-dotsyms", no_argument, NULL, OPTION_NO_DOTSYMS }, | |
7d4c687d AM |
721 | { "save-restore-funcs", no_argument, NULL, OPTION_SAVRES }, |
722 | { "no-save-restore-funcs", no_argument, NULL, OPTION_NO_SAVRES }, | |
e5096e3f | 723 | { "no-tls-optimize", no_argument, NULL, OPTION_NO_TLS_OPT }, |
7c9cf415 | 724 | { "tls-get-addr-optimize", no_argument, NULL, OPTION_TLS_GET_ADDR_OPT }, |
a7f2871e | 725 | { "no-tls-get-addr-optimize", no_argument, NULL, OPTION_NO_TLS_GET_ADDR_OPT }, |
c5614fa4 AM |
726 | { "no-opd-optimize", no_argument, NULL, OPTION_NO_OPD_OPT }, |
727 | { "no-toc-optimize", no_argument, NULL, OPTION_NO_TOC_OPT }, | |
4c52953f | 728 | { "no-multi-toc", no_argument, NULL, OPTION_NO_MULTI_TOC }, |
d43d0b53 | 729 | { "no-toc-sort", no_argument, NULL, OPTION_NO_TOC_SORT }, |
3f764659 | 730 | { "non-overlapping-opd", no_argument, NULL, OPTION_NON_OVERLAPPING_OPD }, |
9c1d81c1 AM |
731 | ' |
732 | ||
58d180e8 | 733 | PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}' |
9c1d81c1 | 734 | fprintf (file, _("\ |
442996ee AM |
735 | --stub-group-size=N Maximum size of a group of input sections that\n\ |
736 | can be handled by one stub section. A negative\n\ | |
737 | value locates all stubs before their branches\n\ | |
738 | (with a group size of -N), while a positive\n\ | |
739 | value allows two groups of input sections, one\n\ | |
740 | before, and one after each stub section.\n\ | |
741 | Values of +/-1 indicate the linker should\n\ | |
742 | choose suitable defaults.\n" | |
9c1d81c1 | 743 | )); |
fac1652d | 744 | fprintf (file, _("\ |
794e51c0 AM |
745 | --plt-static-chain PLT call stubs should load r11.${DEFAULT_PLT_STATIC_CHAIN- (default)}\n" |
746 | )); | |
747 | fprintf (file, _("\ | |
748 | --no-plt-static-chain PLT call stubs should not load r11.${DEFAULT_PLT_STATIC_CHAIN+ (default)}\n" | |
749 | )); | |
750 | fprintf (file, _("\ | |
751 | --plt-thread-safe PLT call stubs with load-load barrier.\n" | |
752 | )); | |
753 | fprintf (file, _("\ | |
754 | --no-plt-thread-safe PLT call stubs without barrier.\n" | |
755 | )); | |
756 | fprintf (file, _("\ | |
757 | --plt-align [=<align>] Align PLT call stubs to fit cache lines.\n" | |
9df0ef5f AM |
758 | )); |
759 | fprintf (file, _("\ | |
794e51c0 | 760 | --no-plt-align Dont'\''t align individual PLT call stubs.\n" |
9df0ef5f AM |
761 | )); |
762 | fprintf (file, _("\ | |
442996ee | 763 | --emit-stub-syms Label linker stubs with a symbol.\n" |
0b9a4d73 AM |
764 | )); |
765 | fprintf (file, _("\ | |
b02c4cfa AM |
766 | --no-emit-stub-syms Don'\''t label linker stubs with a symbol.\n" |
767 | )); | |
768 | fprintf (file, _("\ | |
442996ee AM |
769 | --dotsyms For every version pattern \"foo\" in a version\n\ |
770 | script, add \".foo\" so that function code\n\ | |
771 | symbols are treated the same as function\n\ | |
772 | descriptor symbols. Defaults to on.\n" | |
fac1652d AM |
773 | )); |
774 | fprintf (file, _("\ | |
442996ee | 775 | --no-dotsyms Don'\''t do anything special in version scripts.\n" |
fac1652d | 776 | )); |
e5096e3f | 777 | fprintf (file, _("\ |
7d4c687d AM |
778 | --save-restore-funcs Provide register save and restore routines used\n\ |
779 | by gcc -Os code. Defaults to on for normal\n\ | |
780 | final link, off for ld -r.\n" | |
781 | )); | |
782 | fprintf (file, _("\ | |
783 | --no-save-restore-funcs Don'\''t provide these routines.\n" | |
784 | )); | |
785 | fprintf (file, _("\ | |
442996ee | 786 | --no-tls-optimize Don'\''t try to optimize TLS accesses.\n" |
e5096e3f | 787 | )); |
3f764659 | 788 | fprintf (file, _("\ |
7c9cf415 AM |
789 | --tls-get-addr-optimize Force use of special __tls_get_addr call.\n" |
790 | )); | |
791 | fprintf (file, _("\ | |
a7f2871e AM |
792 | --no-tls-get-addr-optimize Don'\''t use a special __tls_get_addr call.\n" |
793 | )); | |
794 | fprintf (file, _("\ | |
442996ee | 795 | --no-opd-optimize Don'\''t optimize the OPD section.\n" |
c5614fa4 AM |
796 | )); |
797 | fprintf (file, _("\ | |
442996ee | 798 | --no-toc-optimize Don'\''t optimize the TOC section.\n" |
c5614fa4 AM |
799 | )); |
800 | fprintf (file, _("\ | |
442996ee | 801 | --no-multi-toc Disallow automatic multiple toc sections.\n" |
4c52953f AM |
802 | )); |
803 | fprintf (file, _("\ | |
d43d0b53 AM |
804 | --no-toc-sort Don'\''t sort TOC and GOT sections.\n" |
805 | )); | |
806 | fprintf (file, _("\ | |
442996ee AM |
807 | --non-overlapping-opd Canonicalize .opd, so that there are no\n\ |
808 | overlapping .opd entries.\n" | |
3f764659 | 809 | )); |
9c1d81c1 AM |
810 | ' |
811 | ||
58d180e8 | 812 | PARSE_AND_LIST_ARGS_CASES=${PARSE_AND_LIST_ARGS_CASES}' |
9c1d81c1 AM |
813 | case OPTION_STUBGROUP_SIZE: |
814 | { | |
815 | const char *end; | |
e7d1c40c | 816 | params.group_size = bfd_scan_vma (optarg, &end, 0); |
9c1d81c1 AM |
817 | if (*end) |
818 | einfo (_("%P%F: invalid number `%s'\''\n"), optarg); | |
819 | } | |
820 | break; | |
fac1652d | 821 | |
9df0ef5f | 822 | case OPTION_PLT_STATIC_CHAIN: |
e7d1c40c | 823 | params.plt_static_chain = 1; |
9df0ef5f AM |
824 | break; |
825 | ||
826 | case OPTION_NO_PLT_STATIC_CHAIN: | |
e7d1c40c | 827 | params.plt_static_chain = 0; |
9df0ef5f AM |
828 | break; |
829 | ||
794e51c0 | 830 | case OPTION_PLT_THREAD_SAFE: |
e7d1c40c | 831 | params.plt_thread_safe = 1; |
794e51c0 AM |
832 | break; |
833 | ||
834 | case OPTION_NO_PLT_THREAD_SAFE: | |
e7d1c40c | 835 | params.plt_thread_safe = 0; |
794e51c0 AM |
836 | break; |
837 | ||
838 | case OPTION_PLT_ALIGN: | |
839 | if (optarg != NULL) | |
840 | { | |
841 | char *end; | |
842 | unsigned long val = strtoul (optarg, &end, 0); | |
843 | if (*end || val > 8) | |
844 | einfo (_("%P%F: invalid --plt-align `%s'\''\n"), optarg); | |
e05fa0ba | 845 | params.plt_stub_align = val; |
794e51c0 AM |
846 | } |
847 | else | |
e05fa0ba | 848 | params.plt_stub_align = 5; |
794e51c0 AM |
849 | break; |
850 | ||
851 | case OPTION_NO_PLT_ALIGN: | |
e05fa0ba | 852 | params.plt_stub_align = 0; |
794e51c0 AM |
853 | break; |
854 | ||
0b9a4d73 | 855 | case OPTION_STUBSYMS: |
e7d1c40c | 856 | params.emit_stub_syms = 1; |
0b9a4d73 AM |
857 | break; |
858 | ||
b02c4cfa | 859 | case OPTION_NO_STUBSYMS: |
e7d1c40c | 860 | params.emit_stub_syms = 0; |
b02c4cfa AM |
861 | break; |
862 | ||
fac1652d | 863 | case OPTION_DOTSYMS: |
e5096e3f | 864 | dotsyms = 1; |
fac1652d AM |
865 | break; |
866 | ||
867 | case OPTION_NO_DOTSYMS: | |
e5096e3f AM |
868 | dotsyms = 0; |
869 | break; | |
870 | ||
7d4c687d AM |
871 | case OPTION_SAVRES: |
872 | params.save_restore_funcs = 1; | |
873 | break; | |
874 | ||
875 | case OPTION_NO_SAVRES: | |
876 | params.save_restore_funcs = 0; | |
877 | break; | |
878 | ||
e5096e3f | 879 | case OPTION_NO_TLS_OPT: |
c5614fa4 AM |
880 | no_tls_opt = 1; |
881 | break; | |
882 | ||
7c9cf415 AM |
883 | case OPTION_TLS_GET_ADDR_OPT: |
884 | params.tls_get_addr_opt = 1; | |
885 | break; | |
886 | ||
a7f2871e | 887 | case OPTION_NO_TLS_GET_ADDR_OPT: |
7c9cf415 | 888 | params.tls_get_addr_opt = 0; |
a7f2871e AM |
889 | break; |
890 | ||
c5614fa4 AM |
891 | case OPTION_NO_OPD_OPT: |
892 | no_opd_opt = 1; | |
893 | break; | |
894 | ||
895 | case OPTION_NO_TOC_OPT: | |
896 | no_toc_opt = 1; | |
fac1652d | 897 | break; |
3f764659 | 898 | |
4c52953f | 899 | case OPTION_NO_MULTI_TOC: |
e7d1c40c | 900 | params.no_multi_toc = 1; |
4c52953f AM |
901 | break; |
902 | ||
d43d0b53 AM |
903 | case OPTION_NO_TOC_SORT: |
904 | no_toc_sort = 1; | |
905 | break; | |
906 | ||
3f764659 | 907 | case OPTION_NON_OVERLAPPING_OPD: |
e7d1c40c | 908 | params.non_overlapping_opd = 1; |
3f764659 | 909 | break; |
f05eb3b7 AM |
910 | |
911 | case OPTION_TRADITIONAL_FORMAT: | |
912 | no_tls_opt = 1; | |
7c9cf415 | 913 | params.tls_get_addr_opt = 0; |
f05eb3b7 AM |
914 | no_opd_opt = 1; |
915 | no_toc_opt = 1; | |
e7d1c40c | 916 | params.no_multi_toc = 1; |
f05eb3b7 | 917 | no_toc_sort = 1; |
e7d1c40c | 918 | params.plt_static_chain = 1; |
f05eb3b7 | 919 | return FALSE; |
9c1d81c1 AM |
920 | ' |
921 | ||
1f808cd5 AM |
922 | # Put these extra ppc64elf routines in ld_${EMULATION_NAME}_emulation |
923 | # | |
dbd1e97e AM |
924 | LDEMUL_NEW_VERS_PATTERN=gld${EMULATION_NAME}_new_vers_pattern |
925 | LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=ppc_create_output_section_statements | |
926 | LDEMUL_AFTER_OPEN=ppc_after_open | |
836c6af1 | 927 | LDEMUL_BEFORE_ALLOCATION=ppc_before_allocation |
1f808cd5 | 928 | LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation |
8ded5a0f | 929 | LDEMUL_FINISH=gld${EMULATION_NAME}_finish |