PowerPC PLT speculative execution barriers
[deliverable/binutils-gdb.git] / ld / emultempl / ppc32elf.em
1 # This shell script emits a C file. -*- C -*-
2 # Copyright (C) 2003-2018 Free Software Foundation, Inc.
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
22 # This file is sourced from elf32.em, and defines extra powerpc32-elf
23 # specific routines.
24 #
25 fragment <<EOF
26
27 #include "elf32-ppc.h"
28 #include "ldlex.h"
29 #include "ldlang.h"
30
31 #define is_ppc_elf(bfd) \
32 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
33 && elf_object_id (bfd) == PPC32_ELF_DATA)
34
35 /* Whether to run tls optimization. */
36 static int notlsopt = 0;
37
38 /* Choose the correct place for .got. */
39 static int old_got = 0;
40
41 static struct ppc_elf_params params = { PLT_UNSET, 0, 1, -1,
42 0, 0, 0, 0, 0, 0, 0 };
43
44 static void
45 ppc_after_open_output (void)
46 {
47 if (params.emit_stub_syms < 0)
48 params.emit_stub_syms = (link_info.emitrelocations
49 || bfd_link_pic (&link_info));
50 if (params.pagesize == 0)
51 params.pagesize = config.commonpagesize;
52 ppc_elf_link_params (&link_info, &params);
53 }
54
55 EOF
56
57 # No --secure-plt, --bss-plt, or --sdata-got for vxworks.
58 if test -z "$VXWORKS_BASE_EM_FILE" ; then
59 fragment <<EOF
60 static void
61 ppc_after_check_relocs (void)
62 {
63 if (is_ppc_elf (link_info.output_bfd))
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
73 new_plt = ppc_elf_select_plt_layout (link_info.output_bfd, &link_info);
74 if (new_plt < 0)
75 einfo (_("%X%P: select_plt_layout problem %E\n"));
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
112 after_check_relocs_default ();
113 }
114
115 EOF
116 fi
117 fragment <<EOF
118 static void
119 ppc_before_allocation (void)
120 {
121 if (is_ppc_elf (link_info.output_bfd))
122 {
123 if (ppc_elf_tls_setup (link_info.output_bfd, &link_info)
124 && !notlsopt)
125 {
126 if (!ppc_elf_tls_optimize (link_info.output_bfd, &link_info))
127 {
128 einfo (_("%X%P: TLS problem %E\n"));
129 return;
130 }
131 }
132 }
133
134 gld${EMULATION_NAME}_before_allocation ();
135
136 ppc_elf_maybe_strip_sdata_syms (&link_info);
137
138 if (RELAXATION_ENABLED)
139 params.branch_trampolines = 1;
140
141 /* Turn on relaxation if executable sections have addresses that
142 might make branches overflow. */
143 else if (!RELAXATION_DISABLED_BY_USER)
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;
153 expld.dataseg.phase = exp_seg_none;
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;
162 if (o->rawsize == 0)
163 continue;
164 if (low > o->vma)
165 low = o->vma;
166 if (high < o->vma + o->rawsize - 1)
167 high = o->vma + o->rawsize - 1;
168 }
169 if (high > low && high - low > (1 << 25) - 1)
170 params.branch_trampolines = 1;
171 }
172
173 if (params.branch_trampolines
174 || params.ppc476_workaround
175 || params.pic_fixup > 0)
176 ENABLE_RELAXATION;
177 }
178
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
186 static void
187 no_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
210 static void
211 ppc_finish (void)
212 {
213 if (params.ppc476_workaround)
214 lang_for_each_statement (no_zero_padding);
215 finish_default ();
216 }
217
218 EOF
219
220 if grep -q 'ld_elf32_spu_emulation' ldemul-list.h; then
221 fragment <<EOF
222 /* Special handling for embedded SPU executables. */
223 extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *);
224 static bfd_boolean gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *);
225
226 static bfd_boolean
227 ppc_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
235 EOF
236 LDEMUL_RECOGNIZED_FILE=ppc_recognized_file
237 fi
238
239 # Define some shell vars to insert bits of code into the standard elf
240 # parse_args and list_options functions.
241 #
242 PARSE_AND_LIST_PROLOGUE=${PARSE_AND_LIST_PROLOGUE}'
243 enum ppc32_opt
244 {
245 OPTION_NO_TLS_OPT = 321,
246 OPTION_NO_TLS_GET_ADDR_OPT,
247 OPTION_NEW_PLT,
248 OPTION_OLD_PLT,
249 OPTION_SPECULATE_INDIRECT_JUMPS,
250 OPTION_NO_SPECULATE_INDIRECT_JUMPS,
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 };
261 '
262
263 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
264 { "emit-stub-syms", no_argument, NULL, OPTION_STUBSYMS },
265 { "no-emit-stub-syms", no_argument, NULL, OPTION_NO_STUBSYMS },
266 { "no-tls-optimize", no_argument, NULL, OPTION_NO_TLS_OPT },
267 { "no-tls-get-addr-optimize", no_argument, NULL, OPTION_NO_TLS_GET_ADDR_OPT },'
268 if test -z "$VXWORKS_BASE_EM_FILE" ; then
269 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
270 { "secure-plt", no_argument, NULL, OPTION_NEW_PLT },
271 { "bss-plt", no_argument, NULL, OPTION_OLD_PLT },
272 { "speculate-indirect-jumps", no_argument, NULL, OPTION_SPECULATE_INDIRECT_JUMPS },
273 { "no-speculate-indirect-jumps", no_argument, NULL, OPTION_NO_SPECULATE_INDIRECT_JUMPS },
274 { "plt-align", no_argument, NULL, OPTION_PLT_ALIGN },
275 { "no-plt-align", no_argument, NULL, OPTION_NO_PLT_ALIGN },
276 { "sdata-got", no_argument, NULL, OPTION_OLD_GOT },'
277 fi
278 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
279 { "ppc476-workaround", optional_argument, NULL, OPTION_PPC476_WORKAROUND },
280 { "no-ppc476-workaround", no_argument, NULL, OPTION_NO_PPC476_WORKAROUND },
281 { "no-pic-fixup", no_argument, NULL, OPTION_NO_PICFIXUP },
282 { "vle-reloc-fixup", no_argument, NULL, OPTION_VLE_RELOC_FIXUP },
283 '
284
285 PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'
286 fprintf (file, _("\
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 ));'
298 if test -z "$VXWORKS_BASE_EM_FILE" ; then
299 PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'\
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, _("\
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, _("\
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 ));'
321 fi
322 PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'\
323 fprintf (file, _("\
324 --ppc476-workaround [=pagesize]\n\
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, _("\
334 --vle-reloc-fixup Correct old object file 16A/16D relocation.\n"
335 ));
336 '
337
338 PARSE_AND_LIST_ARGS_CASES=${PARSE_AND_LIST_ARGS_CASES}'
339 case OPTION_STUBSYMS:
340 params.emit_stub_syms = 1;
341 break;
342
343 case OPTION_NO_STUBSYMS:
344 params.emit_stub_syms = 0;
345 break;
346
347 case OPTION_NO_TLS_OPT:
348 notlsopt = 1;
349 break;
350
351 case OPTION_NO_TLS_GET_ADDR_OPT:
352 params.no_tls_get_addr_opt = 1;
353 break;
354
355 case OPTION_NEW_PLT:
356 params.plt_style = PLT_NEW;
357 break;
358
359 case OPTION_OLD_PLT:
360 params.plt_style = PLT_OLD;
361 break;
362
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
371 case OPTION_PLT_ALIGN:
372 params.plt_stub_align = 5;
373 break;
374
375 case OPTION_NO_PLT_ALIGN:
376 params.plt_stub_align = 0;
377 break;
378
379 case OPTION_OLD_GOT:
380 old_got = 1;
381 break;
382
383 case OPTION_TRADITIONAL_FORMAT:
384 notlsopt = 1;
385 params.no_tls_get_addr_opt = 1;
386 return FALSE;
387
388 case OPTION_PPC476_WORKAROUND:
389 params.ppc476_workaround = 1;
390 if (optarg != NULL)
391 {
392 char *end;
393 params.pagesize = strtoul (optarg, &end, 0);
394 if (*end
395 || (params.pagesize < 4096 && params.pagesize != 0)
396 || params.pagesize != (params.pagesize & -params.pagesize))
397 einfo (_("%P%F: invalid pagesize `%s'\''\n"), optarg);
398 }
399 break;
400
401 case OPTION_NO_PPC476_WORKAROUND:
402 params.ppc476_workaround = 0;
403 break;
404
405 case OPTION_NO_PICFIXUP:
406 params.pic_fixup = -1;
407 break;
408
409 case OPTION_VLE_RELOC_FIXUP:
410 params.vle_reloc_fixup = 1;
411 break;
412 '
413
414 # Put these extra ppc32elf routines in ld_${EMULATION_NAME}_emulation
415 #
416 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=ppc_after_open_output
417 if test -z "$VXWORKS_BASE_EM_FILE" ; then
418 LDEMUL_AFTER_CHECK_RELOCS=ppc_after_check_relocs
419 fi
420 LDEMUL_BEFORE_ALLOCATION=ppc_before_allocation
421 LDEMUL_FINISH=ppc_finish
This page took 0.045061 seconds and 4 git commands to generate.