PowerPC PLT stub alignment fixes
[deliverable/binutils-gdb.git] / ld / emultempl / ppc32elf.em
CommitLineData
f9e6bfa8 1# This shell script emits a C file. -*- C -*-
219d1afa 2# Copyright (C) 2003-2018 Free Software Foundation, Inc.
f9e6bfa8 3#
f96b4a7b 4# This file is part of the GNU Binutils.
f9e6bfa8
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
f9e6bfa8
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.
f9e6bfa8
AM
20#
21
c9a2f333 22# This file is sourced from elf32.em, and defines extra powerpc32-elf
f9e6bfa8
AM
23# specific routines.
24#
92b93329 25fragment <<EOF
f9e6bfa8 26
f9e6bfa8 27#include "elf32-ppc.h"
f05eb3b7 28#include "ldlex.h"
b4ab4364 29#include "ldlang.h"
f9e6bfa8 30
5503fea1
AM
31#define is_ppc_elf(bfd) \
32 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 33 && elf_object_id (bfd) == PPC32_ELF_DATA)
c9a2f333 34
f9e6bfa8
AM
35/* Whether to run tls optimization. */
36static int notlsopt = 0;
37
5446cbdf 38/* Choose the correct place for .got. */
0cf7d72c
AM
39static int old_got = 0;
40
1be5d8d3 41static struct ppc_elf_params params = { PLT_UNSET, 0, 1, -1,
9e390558 42 0, 0, 0, 0, 0, 0, 0 };
5446cbdf
AM
43
44static void
45ppc_after_open_output (void)
46{
47 if (params.emit_stub_syms < 0)
0e1862bb
L
48 params.emit_stub_syms = (link_info.emitrelocations
49 || bfd_link_pic (&link_info));
76e7a751
AM
50 if (params.pagesize == 0)
51 params.pagesize = config.commonpagesize;
5446cbdf
AM
52 ppc_elf_link_params (&link_info, &params);
53}
54
db434ba0
AM
55EOF
56
57# No --secure-plt, --bss-plt, or --sdata-got for vxworks.
58if test -z "$VXWORKS_BASE_EM_FILE" ; then
59 fragment <<EOF
0cf7d72c 60static void
5c3261b0 61ppc_after_check_relocs (void)
0cf7d72c 62{
5503fea1 63 if (is_ppc_elf (link_info.output_bfd))
0cf7d72c
AM
64 {
65 int new_plt;
66 int keep_new;
67 unsigned int num_plt;
68 unsigned int num_got;
69 lang_output_section_statement_type *os;
70 lang_output_section_statement_type *plt_os[2];
71 lang_output_section_statement_type *got_os[2];
72
5446cbdf 73 new_plt = ppc_elf_select_plt_layout (link_info.output_bfd, &link_info);
0cf7d72c 74 if (new_plt < 0)
d003af55 75 einfo (_("%X%P: select_plt_layout problem %E\n"));
0cf7d72c
AM
76
77 num_got = 0;
78 num_plt = 0;
79 for (os = &lang_output_section_statement.head->output_section_statement;
80 os != NULL;
81 os = os->next)
82 {
83 if (os->constraint == SPECIAL && strcmp (os->name, ".plt") == 0)
84 {
85 if (num_plt < 2)
86 plt_os[num_plt] = os;
87 ++num_plt;
88 }
89 if (os->constraint == SPECIAL && strcmp (os->name, ".got") == 0)
90 {
91 if (num_got < 2)
92 got_os[num_got] = os;
93 ++num_got;
94 }
95 }
96
97 keep_new = new_plt == 1 ? 0 : -1;
98 if (num_plt == 2)
99 {
100 plt_os[0]->constraint = keep_new;
101 plt_os[1]->constraint = ~keep_new;
102 }
103 if (num_got == 2)
104 {
105 if (old_got)
106 keep_new = -1;
107 got_os[0]->constraint = keep_new;
108 got_os[1]->constraint = ~keep_new;
109 }
110 }
111
5c3261b0 112 after_check_relocs_default ();
0cf7d72c
AM
113}
114
db434ba0
AM
115EOF
116fi
117fragment <<EOF
f9e6bfa8 118static void
7e5d8d48 119ppc_before_allocation (void)
f9e6bfa8 120{
5503fea1 121 if (is_ppc_elf (link_info.output_bfd))
f9e6bfa8 122 {
5446cbdf 123 if (ppc_elf_tls_setup (link_info.output_bfd, &link_info)
a7f2871e 124 && !notlsopt)
f9e6bfa8 125 {
f13a99db 126 if (!ppc_elf_tls_optimize (link_info.output_bfd, &link_info))
f9e6bfa8 127 {
d003af55 128 einfo (_("%X%P: TLS problem %E\n"));
f9e6bfa8
AM
129 return;
130 }
131 }
132 }
4135c73b 133
f9e6bfa8 134 gld${EMULATION_NAME}_before_allocation ();
4135c73b 135
93d1b056
AM
136 ppc_elf_maybe_strip_sdata_syms (&link_info);
137
5446cbdf
AM
138 if (RELAXATION_ENABLED)
139 params.branch_trampolines = 1;
140
4135c73b
AM
141 /* Turn on relaxation if executable sections have addresses that
142 might make branches overflow. */
5446cbdf 143 else if (!RELAXATION_DISABLED_BY_USER)
4135c73b
AM
144 {
145 bfd_vma low = (bfd_vma) -1;
146 bfd_vma high = 0;
147 asection *o;
148
149 /* Run lang_size_sections (if not already done). */
150 if (expld.phase != lang_mark_phase_enum)
151 {
152 expld.phase = lang_mark_phase_enum;
58e6cc77 153 expld.dataseg.phase = exp_seg_none;
4135c73b
AM
154 one_lang_size_sections_pass (NULL, FALSE);
155 lang_reset_memory_regions ();
156 }
157
158 for (o = link_info.output_bfd->sections; o != NULL; o = o->next)
159 {
160 if ((o->flags & (SEC_ALLOC | SEC_CODE)) != (SEC_ALLOC | SEC_CODE))
161 continue;
07088e95 162 if (o->rawsize == 0)
4135c73b
AM
163 continue;
164 if (low > o->vma)
165 low = o->vma;
07088e95
AM
166 if (high < o->vma + o->rawsize - 1)
167 high = o->vma + o->rawsize - 1;
4135c73b
AM
168 }
169 if (high > low && high - low > (1 << 25) - 1)
5446cbdf 170 params.branch_trampolines = 1;
4135c73b 171 }
5446cbdf 172
d3e454b9
AM
173 if (params.branch_trampolines
174 || params.ppc476_workaround
175 || params.pic_fixup > 0)
5446cbdf 176 ENABLE_RELAXATION;
f9e6bfa8
AM
177}
178
b4ab4364
AM
179/* Replaces default zero fill padding in executable sections with
180 "ba 0" instructions. This works around the ppc476 icache bug if we
181 have a function pointer tail call near the end of a page, some
182 small amount of padding, then the function called at the beginning
183 of the next page. If the "ba 0" is ever executed we should hit a
184 segv, so it's almost as good as an illegal instruction (zero). */
185
186static void
187no_zero_padding (lang_statement_union_type *l)
188{
189 if (l->header.type == lang_padding_statement_enum
190 && l->padding_statement.size != 0
191 && l->padding_statement.output_section != NULL
192 && (l->padding_statement.output_section->flags & SEC_CODE) != 0
193 && l->padding_statement.fill->size == 0)
194 {
195 struct _ppc_fill_type
196 {
197 size_t size;
198 unsigned char data[4];
199 };
200 static struct _ppc_fill_type fill_be = { 4, {0x48, 0, 0, 2} };
201 static struct _ppc_fill_type fill_le = { 4, {2, 0, 0, 0x48} };
202
203 if (bfd_big_endian (link_info.output_bfd))
204 l->padding_statement.fill = (struct _fill_type *) &fill_be;
205 else
206 l->padding_statement.fill = (struct _fill_type *) &fill_le;
207 }
208}
209
210static void
211ppc_finish (void)
212{
213 if (params.ppc476_workaround)
214 lang_for_each_statement (no_zero_padding);
215 finish_default ();
216}
217
f9e6bfa8
AM
218EOF
219
dc27aea4 220if grep -q 'ld_elf32_spu_emulation' ldemul-list.h; then
92b93329 221 fragment <<EOF
dc27aea4
AM
222/* Special handling for embedded SPU executables. */
223extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *);
224static bfd_boolean gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *);
225
226static bfd_boolean
227ppc_recognized_file (lang_input_statement_type *entry)
228{
229 if (embedded_spu_file (entry, "-m32"))
230 return TRUE;
231
232 return gld${EMULATION_NAME}_load_symbols (entry);
233}
234
235EOF
236LDEMUL_RECOGNIZED_FILE=ppc_recognized_file
237fi
238
f9e6bfa8
AM
239# Define some shell vars to insert bits of code into the standard elf
240# parse_args and list_options functions.
241#
58d180e8 242PARSE_AND_LIST_PROLOGUE=${PARSE_AND_LIST_PROLOGUE}'
9e390558
AM
243enum ppc32_opt
244{
245 OPTION_NO_TLS_OPT = 321,
246 OPTION_NO_TLS_GET_ADDR_OPT,
247 OPTION_NEW_PLT,
248 OPTION_OLD_PLT,
1be5d8d3
AM
249 OPTION_SPECULATE_INDIRECT_JUMPS,
250 OPTION_NO_SPECULATE_INDIRECT_JUMPS,
9e390558
AM
251 OPTION_PLT_ALIGN,
252 OPTION_NO_PLT_ALIGN,
253 OPTION_OLD_GOT,
254 OPTION_STUBSYMS,
255 OPTION_NO_STUBSYMS,
256 OPTION_PPC476_WORKAROUND,
257 OPTION_NO_PPC476_WORKAROUND,
258 OPTION_NO_PICFIXUP,
259 OPTION_VLE_RELOC_FIXUP
260};
f9e6bfa8
AM
261'
262
58d180e8 263PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
0ba07910 264 { "emit-stub-syms", no_argument, NULL, OPTION_STUBSYMS },
b02c4cfa 265 { "no-emit-stub-syms", no_argument, NULL, OPTION_NO_STUBSYMS },
f9e6bfa8 266 { "no-tls-optimize", no_argument, NULL, OPTION_NO_TLS_OPT },
db434ba0
AM
267 { "no-tls-get-addr-optimize", no_argument, NULL, OPTION_NO_TLS_GET_ADDR_OPT },'
268if test -z "$VXWORKS_BASE_EM_FILE" ; then
269 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
016687f8 270 { "secure-plt", no_argument, NULL, OPTION_NEW_PLT },
0cf7d72c 271 { "bss-plt", no_argument, NULL, OPTION_OLD_PLT },
1be5d8d3
AM
272 { "speculate-indirect-jumps", no_argument, NULL, OPTION_SPECULATE_INDIRECT_JUMPS },
273 { "no-speculate-indirect-jumps", no_argument, NULL, OPTION_NO_SPECULATE_INDIRECT_JUMPS },
691d2e9a 274 { "plt-align", optional_argument, NULL, OPTION_PLT_ALIGN },
9e390558 275 { "no-plt-align", no_argument, NULL, OPTION_NO_PLT_ALIGN },
db434ba0
AM
276 { "sdata-got", no_argument, NULL, OPTION_OLD_GOT },'
277fi
278PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
5446cbdf
AM
279 { "ppc476-workaround", optional_argument, NULL, OPTION_PPC476_WORKAROUND },
280 { "no-ppc476-workaround", no_argument, NULL, OPTION_NO_PPC476_WORKAROUND },
d3e454b9 281 { "no-pic-fixup", no_argument, NULL, OPTION_NO_PICFIXUP },
08dc996f 282 { "vle-reloc-fixup", no_argument, NULL, OPTION_VLE_RELOC_FIXUP },
f9e6bfa8
AM
283'
284
58d180e8 285PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'
f9e6bfa8 286 fprintf (file, _("\
9e390558
AM
287 --emit-stub-syms Label linker stubs with a symbol.\n"
288 ));
289 fprintf (file, _("\
290 --no-emit-stub-syms Don'\''t label linker stubs with a symbol.\n"
291 ));
292 fprintf (file, _("\
293 --no-tls-optimize Don'\''t try to optimize TLS accesses.\n"
294 ));
295 fprintf (file, _("\
296 --no-tls-get-addr-optimize Don'\''t use a special __tls_get_addr call.\n"
297 ));'
db434ba0
AM
298if test -z "$VXWORKS_BASE_EM_FILE" ; then
299 PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'\
9e390558
AM
300 fprintf (file, _("\
301 --secure-plt Use new-style PLT if possible.\n"
302 ));
303 fprintf (file, _("\
304 --bss-plt Force old-style BSS PLT.\n"
305 ));
306 fprintf (file, _("\
1be5d8d3
AM
307 --speculate-indirect-jumps PLT call stubs without speculation barrier.\n"
308 ));
309 fprintf (file, _("\
310 --no-speculate-indirect-jumps PLT call stubs with speculation barrier.\n"
311 ));
312 fprintf (file, _("\
9e390558
AM
313 --plt-align Align PLT call stubs to fit cache lines.\n"
314 ));
315 fprintf (file, _("\
316 --no-plt-align Dont'\''t align individual PLT call stubs.\n"
317 ));
318 fprintf (file, _("\
319 --sdata-got Force GOT location just before .sdata.\n"
320 ));'
db434ba0
AM
321fi
322PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'\
9e390558 323 fprintf (file, _("\
5446cbdf 324 --ppc476-workaround [=pagesize]\n\
9e390558
AM
325 Avoid a cache bug on ppc476.\n"
326 ));
327 fprintf (file, _("\
328 --no-ppc476-workaround Disable workaround.\n"
329 ));
330 fprintf (file, _("\
331 --no-pic-fixup Don'\''t edit non-pic to pic.\n"
332 ));
333 fprintf (file, _("\
08dc996f 334 --vle-reloc-fixup Correct old object file 16A/16D relocation.\n"
f9e6bfa8
AM
335 ));
336'
337
58d180e8 338PARSE_AND_LIST_ARGS_CASES=${PARSE_AND_LIST_ARGS_CASES}'
0ba07910 339 case OPTION_STUBSYMS:
5446cbdf 340 params.emit_stub_syms = 1;
0ba07910
AM
341 break;
342
b02c4cfa 343 case OPTION_NO_STUBSYMS:
5446cbdf 344 params.emit_stub_syms = 0;
b02c4cfa
AM
345 break;
346
f9e6bfa8
AM
347 case OPTION_NO_TLS_OPT:
348 notlsopt = 1;
349 break;
0cf7d72c 350
a7f2871e 351 case OPTION_NO_TLS_GET_ADDR_OPT:
5446cbdf 352 params.no_tls_get_addr_opt = 1;
a7f2871e
AM
353 break;
354
016687f8 355 case OPTION_NEW_PLT:
5446cbdf 356 params.plt_style = PLT_NEW;
016687f8
AM
357 break;
358
0cf7d72c 359 case OPTION_OLD_PLT:
5446cbdf 360 params.plt_style = PLT_OLD;
0cf7d72c
AM
361 break;
362
1be5d8d3
AM
363 case OPTION_SPECULATE_INDIRECT_JUMPS:
364 params.speculate_indirect_jumps = 1;
365 break;
366
367 case OPTION_NO_SPECULATE_INDIRECT_JUMPS:
368 params.speculate_indirect_jumps = 0;
369 break;
370
9e390558 371 case OPTION_PLT_ALIGN:
691d2e9a
AM
372 if (optarg != NULL)
373 {
374 char *end;
375 unsigned long val = strtoul (optarg, &end, 0);
376 if (*end || val > 5)
377 einfo (_("%P%F: invalid --plt-align `%s'\''\n"), optarg);
378 params.plt_stub_align = val;
379 }
380 else
381 params.plt_stub_align = 5;
9e390558
AM
382 break;
383
384 case OPTION_NO_PLT_ALIGN:
385 params.plt_stub_align = 0;
386 break;
387
0cf7d72c
AM
388 case OPTION_OLD_GOT:
389 old_got = 1;
390 break;
f05eb3b7
AM
391
392 case OPTION_TRADITIONAL_FORMAT:
393 notlsopt = 1;
5446cbdf 394 params.no_tls_get_addr_opt = 1;
f05eb3b7 395 return FALSE;
5446cbdf
AM
396
397 case OPTION_PPC476_WORKAROUND:
398 params.ppc476_workaround = 1;
399 if (optarg != NULL)
400 {
401 char *end;
76e7a751 402 params.pagesize = strtoul (optarg, &end, 0);
5446cbdf 403 if (*end
76e7a751
AM
404 || (params.pagesize < 4096 && params.pagesize != 0)
405 || params.pagesize != (params.pagesize & -params.pagesize))
5446cbdf
AM
406 einfo (_("%P%F: invalid pagesize `%s'\''\n"), optarg);
407 }
408 break;
409
410 case OPTION_NO_PPC476_WORKAROUND:
411 params.ppc476_workaround = 0;
412 break;
d3e454b9
AM
413
414 case OPTION_NO_PICFIXUP:
415 params.pic_fixup = -1;
416 break;
08dc996f
AM
417
418 case OPTION_VLE_RELOC_FIXUP:
419 params.vle_reloc_fixup = 1;
420 break;
f9e6bfa8
AM
421'
422
c9a2f333 423# Put these extra ppc32elf routines in ld_${EMULATION_NAME}_emulation
f9e6bfa8 424#
5446cbdf 425LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=ppc_after_open_output
db434ba0 426if test -z "$VXWORKS_BASE_EM_FILE" ; then
5c3261b0 427 LDEMUL_AFTER_CHECK_RELOCS=ppc_after_check_relocs
db434ba0 428fi
f9e6bfa8 429LDEMUL_BEFORE_ALLOCATION=ppc_before_allocation
b4ab4364 430LDEMUL_FINISH=ppc_finish
This page took 0.677991 seconds and 4 git commands to generate.