Use input_bfd in relocate_section
[deliverable/binutils-gdb.git] / ld / emultempl / ppc32elf.em
CommitLineData
f9e6bfa8 1# This shell script emits a C file. -*- C -*-
6f2750fe 2# Copyright (C) 2003-2016 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
76e7a751 41static struct ppc_elf_params params = { PLT_UNSET, -1, 0, 0, 0, 0, 0, 0 };
5446cbdf
AM
42
43static void
44ppc_after_open_output (void)
45{
46 if (params.emit_stub_syms < 0)
0e1862bb
L
47 params.emit_stub_syms = (link_info.emitrelocations
48 || bfd_link_pic (&link_info));
76e7a751
AM
49 if (params.pagesize == 0)
50 params.pagesize = config.commonpagesize;
5446cbdf
AM
51 ppc_elf_link_params (&link_info, &params);
52}
53
db434ba0
AM
54EOF
55
56# No --secure-plt, --bss-plt, or --sdata-got for vxworks.
57if test -z "$VXWORKS_BASE_EM_FILE" ; then
58 fragment <<EOF
0cf7d72c
AM
59static void
60ppc_after_open (void)
61{
5503fea1 62 if (is_ppc_elf (link_info.output_bfd))
0cf7d72c
AM
63 {
64 int new_plt;
65 int keep_new;
66 unsigned int num_plt;
67 unsigned int num_got;
68 lang_output_section_statement_type *os;
69 lang_output_section_statement_type *plt_os[2];
70 lang_output_section_statement_type *got_os[2];
71
5446cbdf 72 new_plt = ppc_elf_select_plt_layout (link_info.output_bfd, &link_info);
0cf7d72c
AM
73 if (new_plt < 0)
74 einfo ("%X%P: select_plt_layout problem %E\n");
75
76 num_got = 0;
77 num_plt = 0;
78 for (os = &lang_output_section_statement.head->output_section_statement;
79 os != NULL;
80 os = os->next)
81 {
82 if (os->constraint == SPECIAL && strcmp (os->name, ".plt") == 0)
83 {
84 if (num_plt < 2)
85 plt_os[num_plt] = os;
86 ++num_plt;
87 }
88 if (os->constraint == SPECIAL && strcmp (os->name, ".got") == 0)
89 {
90 if (num_got < 2)
91 got_os[num_got] = os;
92 ++num_got;
93 }
94 }
95
96 keep_new = new_plt == 1 ? 0 : -1;
97 if (num_plt == 2)
98 {
99 plt_os[0]->constraint = keep_new;
100 plt_os[1]->constraint = ~keep_new;
101 }
102 if (num_got == 2)
103 {
104 if (old_got)
105 keep_new = -1;
106 got_os[0]->constraint = keep_new;
107 got_os[1]->constraint = ~keep_new;
108 }
109 }
110
111 gld${EMULATION_NAME}_after_open ();
112}
113
db434ba0
AM
114EOF
115fi
116fragment <<EOF
f9e6bfa8 117static void
7e5d8d48 118ppc_before_allocation (void)
f9e6bfa8 119{
5503fea1 120 if (is_ppc_elf (link_info.output_bfd))
f9e6bfa8 121 {
5446cbdf 122 if (ppc_elf_tls_setup (link_info.output_bfd, &link_info)
a7f2871e 123 && !notlsopt)
f9e6bfa8 124 {
f13a99db 125 if (!ppc_elf_tls_optimize (link_info.output_bfd, &link_info))
f9e6bfa8
AM
126 {
127 einfo ("%X%P: TLS problem %E\n");
128 return;
129 }
130 }
131 }
4135c73b 132
f9e6bfa8 133 gld${EMULATION_NAME}_before_allocation ();
4135c73b 134
93d1b056
AM
135 ppc_elf_maybe_strip_sdata_syms (&link_info);
136
5446cbdf
AM
137 if (RELAXATION_ENABLED)
138 params.branch_trampolines = 1;
139
4135c73b
AM
140 /* Turn on relaxation if executable sections have addresses that
141 might make branches overflow. */
5446cbdf 142 else if (!RELAXATION_DISABLED_BY_USER)
4135c73b
AM
143 {
144 bfd_vma low = (bfd_vma) -1;
145 bfd_vma high = 0;
146 asection *o;
147
148 /* Run lang_size_sections (if not already done). */
149 if (expld.phase != lang_mark_phase_enum)
150 {
151 expld.phase = lang_mark_phase_enum;
152 expld.dataseg.phase = exp_dataseg_none;
153 one_lang_size_sections_pass (NULL, FALSE);
154 lang_reset_memory_regions ();
155 }
156
157 for (o = link_info.output_bfd->sections; o != NULL; o = o->next)
158 {
159 if ((o->flags & (SEC_ALLOC | SEC_CODE)) != (SEC_ALLOC | SEC_CODE))
160 continue;
07088e95 161 if (o->rawsize == 0)
4135c73b
AM
162 continue;
163 if (low > o->vma)
164 low = o->vma;
07088e95
AM
165 if (high < o->vma + o->rawsize - 1)
166 high = o->vma + o->rawsize - 1;
4135c73b
AM
167 }
168 if (high > low && high - low > (1 << 25) - 1)
5446cbdf 169 params.branch_trampolines = 1;
4135c73b 170 }
5446cbdf 171
d3e454b9
AM
172 if (params.branch_trampolines
173 || params.ppc476_workaround
174 || params.pic_fixup > 0)
5446cbdf 175 ENABLE_RELAXATION;
f9e6bfa8
AM
176}
177
b4ab4364
AM
178/* Replaces default zero fill padding in executable sections with
179 "ba 0" instructions. This works around the ppc476 icache bug if we
180 have a function pointer tail call near the end of a page, some
181 small amount of padding, then the function called at the beginning
182 of the next page. If the "ba 0" is ever executed we should hit a
183 segv, so it's almost as good as an illegal instruction (zero). */
184
185static void
186no_zero_padding (lang_statement_union_type *l)
187{
188 if (l->header.type == lang_padding_statement_enum
189 && l->padding_statement.size != 0
190 && l->padding_statement.output_section != NULL
191 && (l->padding_statement.output_section->flags & SEC_CODE) != 0
192 && l->padding_statement.fill->size == 0)
193 {
194 struct _ppc_fill_type
195 {
196 size_t size;
197 unsigned char data[4];
198 };
199 static struct _ppc_fill_type fill_be = { 4, {0x48, 0, 0, 2} };
200 static struct _ppc_fill_type fill_le = { 4, {2, 0, 0, 0x48} };
201
202 if (bfd_big_endian (link_info.output_bfd))
203 l->padding_statement.fill = (struct _fill_type *) &fill_be;
204 else
205 l->padding_statement.fill = (struct _fill_type *) &fill_le;
206 }
207}
208
209static void
210ppc_finish (void)
211{
212 if (params.ppc476_workaround)
213 lang_for_each_statement (no_zero_padding);
214 finish_default ();
215}
216
f9e6bfa8
AM
217EOF
218
dc27aea4 219if grep -q 'ld_elf32_spu_emulation' ldemul-list.h; then
92b93329 220 fragment <<EOF
dc27aea4
AM
221/* Special handling for embedded SPU executables. */
222extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *);
223static bfd_boolean gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *);
224
225static bfd_boolean
226ppc_recognized_file (lang_input_statement_type *entry)
227{
228 if (embedded_spu_file (entry, "-m32"))
229 return TRUE;
230
231 return gld${EMULATION_NAME}_load_symbols (entry);
232}
233
234EOF
235LDEMUL_RECOGNIZED_FILE=ppc_recognized_file
236fi
237
f9e6bfa8
AM
238# Define some shell vars to insert bits of code into the standard elf
239# parse_args and list_options functions.
240#
58d180e8
AM
241PARSE_AND_LIST_PROLOGUE=${PARSE_AND_LIST_PROLOGUE}'
242#define OPTION_NO_TLS_OPT 321
a7f2871e
AM
243#define OPTION_NO_TLS_GET_ADDR_OPT (OPTION_NO_TLS_OPT + 1)
244#define OPTION_NEW_PLT (OPTION_NO_TLS_GET_ADDR_OPT + 1)
245#define OPTION_OLD_PLT (OPTION_NEW_PLT + 1)
246#define OPTION_OLD_GOT (OPTION_OLD_PLT + 1)
247#define OPTION_STUBSYMS (OPTION_OLD_GOT + 1)
b02c4cfa 248#define OPTION_NO_STUBSYMS (OPTION_STUBSYMS + 1)
5446cbdf
AM
249#define OPTION_PPC476_WORKAROUND (OPTION_NO_STUBSYMS + 1)
250#define OPTION_NO_PPC476_WORKAROUND (OPTION_PPC476_WORKAROUND + 1)
d3e454b9 251#define OPTION_NO_PICFIXUP (OPTION_NO_PPC476_WORKAROUND + 1)
f9e6bfa8
AM
252'
253
58d180e8 254PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
0ba07910 255 { "emit-stub-syms", no_argument, NULL, OPTION_STUBSYMS },
b02c4cfa 256 { "no-emit-stub-syms", no_argument, NULL, OPTION_NO_STUBSYMS },
f9e6bfa8 257 { "no-tls-optimize", no_argument, NULL, OPTION_NO_TLS_OPT },
db434ba0
AM
258 { "no-tls-get-addr-optimize", no_argument, NULL, OPTION_NO_TLS_GET_ADDR_OPT },'
259if test -z "$VXWORKS_BASE_EM_FILE" ; then
260 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
016687f8 261 { "secure-plt", no_argument, NULL, OPTION_NEW_PLT },
0cf7d72c 262 { "bss-plt", no_argument, NULL, OPTION_OLD_PLT },
db434ba0
AM
263 { "sdata-got", no_argument, NULL, OPTION_OLD_GOT },'
264fi
265PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
5446cbdf
AM
266 { "ppc476-workaround", optional_argument, NULL, OPTION_PPC476_WORKAROUND },
267 { "no-ppc476-workaround", no_argument, NULL, OPTION_NO_PPC476_WORKAROUND },
d3e454b9 268 { "no-pic-fixup", no_argument, NULL, OPTION_NO_PICFIXUP },
f9e6bfa8
AM
269'
270
58d180e8 271PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'
f9e6bfa8 272 fprintf (file, _("\
442996ee 273 --emit-stub-syms Label linker stubs with a symbol.\n\
b02c4cfa 274 --no-emit-stub-syms Don'\''t label linker stubs with a symbol.\n\
442996ee 275 --no-tls-optimize Don'\''t try to optimize TLS accesses.\n\
db434ba0
AM
276 --no-tls-get-addr-optimize Don'\''t use a special __tls_get_addr call.\n'
277if test -z "$VXWORKS_BASE_EM_FILE" ; then
278 PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'\
442996ee
AM
279 --secure-plt Use new-style PLT if possible.\n\
280 --bss-plt Force old-style BSS PLT.\n\
db434ba0
AM
281 --sdata-got Force GOT location just before .sdata.\n'
282fi
283PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'\
5446cbdf
AM
284 --ppc476-workaround [=pagesize]\n\
285 Avoid a cache bug on ppc476.\n\
d3e454b9
AM
286 --no-ppc476-workaround Disable workaround.\n\
287 --no-pic-fixup Don'\''t edit non-pic to pic.\n"
f9e6bfa8
AM
288 ));
289'
290
58d180e8 291PARSE_AND_LIST_ARGS_CASES=${PARSE_AND_LIST_ARGS_CASES}'
0ba07910 292 case OPTION_STUBSYMS:
5446cbdf 293 params.emit_stub_syms = 1;
0ba07910
AM
294 break;
295
b02c4cfa 296 case OPTION_NO_STUBSYMS:
5446cbdf 297 params.emit_stub_syms = 0;
b02c4cfa
AM
298 break;
299
f9e6bfa8
AM
300 case OPTION_NO_TLS_OPT:
301 notlsopt = 1;
302 break;
0cf7d72c 303
a7f2871e 304 case OPTION_NO_TLS_GET_ADDR_OPT:
5446cbdf 305 params.no_tls_get_addr_opt = 1;
a7f2871e
AM
306 break;
307
016687f8 308 case OPTION_NEW_PLT:
5446cbdf 309 params.plt_style = PLT_NEW;
016687f8
AM
310 break;
311
0cf7d72c 312 case OPTION_OLD_PLT:
5446cbdf 313 params.plt_style = PLT_OLD;
0cf7d72c
AM
314 break;
315
316 case OPTION_OLD_GOT:
317 old_got = 1;
318 break;
f05eb3b7
AM
319
320 case OPTION_TRADITIONAL_FORMAT:
321 notlsopt = 1;
5446cbdf 322 params.no_tls_get_addr_opt = 1;
f05eb3b7 323 return FALSE;
5446cbdf
AM
324
325 case OPTION_PPC476_WORKAROUND:
326 params.ppc476_workaround = 1;
327 if (optarg != NULL)
328 {
329 char *end;
76e7a751 330 params.pagesize = strtoul (optarg, &end, 0);
5446cbdf 331 if (*end
76e7a751
AM
332 || (params.pagesize < 4096 && params.pagesize != 0)
333 || params.pagesize != (params.pagesize & -params.pagesize))
5446cbdf
AM
334 einfo (_("%P%F: invalid pagesize `%s'\''\n"), optarg);
335 }
336 break;
337
338 case OPTION_NO_PPC476_WORKAROUND:
339 params.ppc476_workaround = 0;
340 break;
d3e454b9
AM
341
342 case OPTION_NO_PICFIXUP:
343 params.pic_fixup = -1;
344 break;
f9e6bfa8
AM
345'
346
c9a2f333 347# Put these extra ppc32elf routines in ld_${EMULATION_NAME}_emulation
f9e6bfa8 348#
5446cbdf 349LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=ppc_after_open_output
db434ba0
AM
350if test -z "$VXWORKS_BASE_EM_FILE" ; then
351 LDEMUL_AFTER_OPEN=ppc_after_open
352fi
f9e6bfa8 353LDEMUL_BEFORE_ALLOCATION=ppc_before_allocation
b4ab4364 354LDEMUL_FINISH=ppc_finish
This page took 0.57684 seconds and 4 git commands to generate.