Fix ELF ppc32 targets that don't use ppc32elf.em
[binutils-gdb.git] / ld / emultempl / ppc32elf.em
1 # This shell script emits a C file. -*- C -*-
2 # Copyright (C) 2003-2014 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 "libbfd.h"
28 #include "elf32-ppc.h"
29 #include "ldlex.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 bfd_vma pagesize = 0;
42
43 static struct ppc_elf_params params = { PLT_UNSET, -1, 0, 0, 0, 0 };
44
45 static void
46 ppc_after_open_output (void)
47 {
48 if (params.emit_stub_syms < 0)
49 params.emit_stub_syms = link_info.emitrelocations || link_info.shared;
50 if (pagesize == 0)
51 pagesize = config.commonpagesize;
52 params.pagesize_p2 = bfd_log2 (pagesize);
53 ppc_elf_link_params (&link_info, &params);
54 }
55
56 EOF
57
58 # No --secure-plt, --bss-plt, or --sdata-got for vxworks.
59 if test -z "$VXWORKS_BASE_EM_FILE" ; then
60 fragment <<EOF
61 static void
62 ppc_after_open (void)
63 {
64 if (is_ppc_elf (link_info.output_bfd))
65 {
66 int new_plt;
67 int keep_new;
68 unsigned int num_plt;
69 unsigned int num_got;
70 lang_output_section_statement_type *os;
71 lang_output_section_statement_type *plt_os[2];
72 lang_output_section_statement_type *got_os[2];
73
74 new_plt = ppc_elf_select_plt_layout (link_info.output_bfd, &link_info);
75 if (new_plt < 0)
76 einfo ("%X%P: select_plt_layout problem %E\n");
77
78 num_got = 0;
79 num_plt = 0;
80 for (os = &lang_output_section_statement.head->output_section_statement;
81 os != NULL;
82 os = os->next)
83 {
84 if (os->constraint == SPECIAL && strcmp (os->name, ".plt") == 0)
85 {
86 if (num_plt < 2)
87 plt_os[num_plt] = os;
88 ++num_plt;
89 }
90 if (os->constraint == SPECIAL && strcmp (os->name, ".got") == 0)
91 {
92 if (num_got < 2)
93 got_os[num_got] = os;
94 ++num_got;
95 }
96 }
97
98 keep_new = new_plt == 1 ? 0 : -1;
99 if (num_plt == 2)
100 {
101 plt_os[0]->constraint = keep_new;
102 plt_os[1]->constraint = ~keep_new;
103 }
104 if (num_got == 2)
105 {
106 if (old_got)
107 keep_new = -1;
108 got_os[0]->constraint = keep_new;
109 got_os[1]->constraint = ~keep_new;
110 }
111 }
112
113 gld${EMULATION_NAME}_after_open ();
114 }
115
116 EOF
117 fi
118 fragment <<EOF
119 static void
120 ppc_before_allocation (void)
121 {
122 if (is_ppc_elf (link_info.output_bfd))
123 {
124 if (ppc_elf_tls_setup (link_info.output_bfd, &link_info)
125 && !notlsopt)
126 {
127 if (!ppc_elf_tls_optimize (link_info.output_bfd, &link_info))
128 {
129 einfo ("%X%P: TLS problem %E\n");
130 return;
131 }
132 }
133 }
134
135 gld${EMULATION_NAME}_before_allocation ();
136
137 if (RELAXATION_ENABLED)
138 params.branch_trampolines = 1;
139
140 /* Turn on relaxation if executable sections have addresses that
141 might make branches overflow. */
142 else if (!RELAXATION_DISABLED_BY_USER)
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;
161 if (o->rawsize == 0)
162 continue;
163 if (low > o->vma)
164 low = o->vma;
165 if (high < o->vma + o->rawsize - 1)
166 high = o->vma + o->rawsize - 1;
167 }
168 if (high > low && high - low > (1 << 25) - 1)
169 params.branch_trampolines = 1;
170 }
171
172 if (params.ppc476_workaround || params.branch_trampolines)
173 ENABLE_RELAXATION;
174 }
175
176 EOF
177
178 if grep -q 'ld_elf32_spu_emulation' ldemul-list.h; then
179 fragment <<EOF
180 /* Special handling for embedded SPU executables. */
181 extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *);
182 static bfd_boolean gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *);
183
184 static bfd_boolean
185 ppc_recognized_file (lang_input_statement_type *entry)
186 {
187 if (embedded_spu_file (entry, "-m32"))
188 return TRUE;
189
190 return gld${EMULATION_NAME}_load_symbols (entry);
191 }
192
193 EOF
194 LDEMUL_RECOGNIZED_FILE=ppc_recognized_file
195 fi
196
197 # Define some shell vars to insert bits of code into the standard elf
198 # parse_args and list_options functions.
199 #
200 PARSE_AND_LIST_PROLOGUE=${PARSE_AND_LIST_PROLOGUE}'
201 #define OPTION_NO_TLS_OPT 321
202 #define OPTION_NO_TLS_GET_ADDR_OPT (OPTION_NO_TLS_OPT + 1)
203 #define OPTION_NEW_PLT (OPTION_NO_TLS_GET_ADDR_OPT + 1)
204 #define OPTION_OLD_PLT (OPTION_NEW_PLT + 1)
205 #define OPTION_OLD_GOT (OPTION_OLD_PLT + 1)
206 #define OPTION_STUBSYMS (OPTION_OLD_GOT + 1)
207 #define OPTION_NO_STUBSYMS (OPTION_STUBSYMS + 1)
208 #define OPTION_PPC476_WORKAROUND (OPTION_NO_STUBSYMS + 1)
209 #define OPTION_NO_PPC476_WORKAROUND (OPTION_PPC476_WORKAROUND + 1)
210 '
211
212 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
213 { "emit-stub-syms", no_argument, NULL, OPTION_STUBSYMS },
214 { "no-emit-stub-syms", no_argument, NULL, OPTION_NO_STUBSYMS },
215 { "no-tls-optimize", no_argument, NULL, OPTION_NO_TLS_OPT },
216 { "no-tls-get-addr-optimize", no_argument, NULL, OPTION_NO_TLS_GET_ADDR_OPT },'
217 if test -z "$VXWORKS_BASE_EM_FILE" ; then
218 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
219 { "secure-plt", no_argument, NULL, OPTION_NEW_PLT },
220 { "bss-plt", no_argument, NULL, OPTION_OLD_PLT },
221 { "sdata-got", no_argument, NULL, OPTION_OLD_GOT },'
222 fi
223 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
224 { "ppc476-workaround", optional_argument, NULL, OPTION_PPC476_WORKAROUND },
225 { "no-ppc476-workaround", no_argument, NULL, OPTION_NO_PPC476_WORKAROUND },
226 '
227
228 PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'
229 fprintf (file, _("\
230 --emit-stub-syms Label linker stubs with a symbol.\n\
231 --no-emit-stub-syms Don'\''t label linker stubs with a symbol.\n\
232 --no-tls-optimize Don'\''t try to optimize TLS accesses.\n\
233 --no-tls-get-addr-optimize Don'\''t use a special __tls_get_addr call.\n'
234 if test -z "$VXWORKS_BASE_EM_FILE" ; then
235 PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'\
236 --secure-plt Use new-style PLT if possible.\n\
237 --bss-plt Force old-style BSS PLT.\n\
238 --sdata-got Force GOT location just before .sdata.\n'
239 fi
240 PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'\
241 --ppc476-workaround [=pagesize]\n\
242 Avoid a cache bug on ppc476.\n\
243 --no-ppc476-workaround Disable workaround.\n"
244 ));
245 '
246
247 PARSE_AND_LIST_ARGS_CASES=${PARSE_AND_LIST_ARGS_CASES}'
248 case OPTION_STUBSYMS:
249 params.emit_stub_syms = 1;
250 break;
251
252 case OPTION_NO_STUBSYMS:
253 params.emit_stub_syms = 0;
254 break;
255
256 case OPTION_NO_TLS_OPT:
257 notlsopt = 1;
258 break;
259
260 case OPTION_NO_TLS_GET_ADDR_OPT:
261 params.no_tls_get_addr_opt = 1;
262 break;
263
264 case OPTION_NEW_PLT:
265 params.plt_style = PLT_NEW;
266 break;
267
268 case OPTION_OLD_PLT:
269 params.plt_style = PLT_OLD;
270 break;
271
272 case OPTION_OLD_GOT:
273 old_got = 1;
274 break;
275
276 case OPTION_TRADITIONAL_FORMAT:
277 notlsopt = 1;
278 params.no_tls_get_addr_opt = 1;
279 return FALSE;
280
281 case OPTION_PPC476_WORKAROUND:
282 params.ppc476_workaround = 1;
283 if (optarg != NULL)
284 {
285 char *end;
286 pagesize = strtoul (optarg, &end, 0);
287 if (*end
288 || (pagesize < 4096 && pagesize != 0)
289 || pagesize != (pagesize & -pagesize))
290 einfo (_("%P%F: invalid pagesize `%s'\''\n"), optarg);
291 }
292 break;
293
294 case OPTION_NO_PPC476_WORKAROUND:
295 params.ppc476_workaround = 0;
296 break;
297 '
298
299 # Put these extra ppc32elf routines in ld_${EMULATION_NAME}_emulation
300 #
301 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=ppc_after_open_output
302 if test -z "$VXWORKS_BASE_EM_FILE" ; then
303 LDEMUL_AFTER_OPEN=ppc_after_open
304 fi
305 LDEMUL_BEFORE_ALLOCATION=ppc_before_allocation