Fix freeing of link_info.hash
[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 #include "ldlang.h"
31
32 #define is_ppc_elf(bfd) \
33 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
34 && elf_object_id (bfd) == PPC32_ELF_DATA)
35
36 /* Whether to run tls optimization. */
37 static int notlsopt = 0;
38
39 /* Choose the correct place for .got. */
40 static int old_got = 0;
41
42 static bfd_vma pagesize = 0;
43
44 static struct ppc_elf_params params = { PLT_UNSET, -1, 0, 0, 0, 0 };
45
46 static void
47 ppc_after_open_output (void)
48 {
49 if (params.emit_stub_syms < 0)
50 params.emit_stub_syms = link_info.emitrelocations || link_info.shared;
51 if (pagesize == 0)
52 pagesize = config.commonpagesize;
53 params.pagesize_p2 = bfd_log2 (pagesize);
54 ppc_elf_link_params (&link_info, &params);
55 }
56
57 EOF
58
59 # No --secure-plt, --bss-plt, or --sdata-got for vxworks.
60 if test -z "$VXWORKS_BASE_EM_FILE" ; then
61 fragment <<EOF
62 static void
63 ppc_after_open (void)
64 {
65 if (is_ppc_elf (link_info.output_bfd))
66 {
67 int new_plt;
68 int keep_new;
69 unsigned int num_plt;
70 unsigned int num_got;
71 lang_output_section_statement_type *os;
72 lang_output_section_statement_type *plt_os[2];
73 lang_output_section_statement_type *got_os[2];
74
75 new_plt = ppc_elf_select_plt_layout (link_info.output_bfd, &link_info);
76 if (new_plt < 0)
77 einfo ("%X%P: select_plt_layout problem %E\n");
78
79 num_got = 0;
80 num_plt = 0;
81 for (os = &lang_output_section_statement.head->output_section_statement;
82 os != NULL;
83 os = os->next)
84 {
85 if (os->constraint == SPECIAL && strcmp (os->name, ".plt") == 0)
86 {
87 if (num_plt < 2)
88 plt_os[num_plt] = os;
89 ++num_plt;
90 }
91 if (os->constraint == SPECIAL && strcmp (os->name, ".got") == 0)
92 {
93 if (num_got < 2)
94 got_os[num_got] = os;
95 ++num_got;
96 }
97 }
98
99 keep_new = new_plt == 1 ? 0 : -1;
100 if (num_plt == 2)
101 {
102 plt_os[0]->constraint = keep_new;
103 plt_os[1]->constraint = ~keep_new;
104 }
105 if (num_got == 2)
106 {
107 if (old_got)
108 keep_new = -1;
109 got_os[0]->constraint = keep_new;
110 got_os[1]->constraint = ~keep_new;
111 }
112 }
113
114 gld${EMULATION_NAME}_after_open ();
115 }
116
117 EOF
118 fi
119 fragment <<EOF
120 static void
121 ppc_before_allocation (void)
122 {
123 if (is_ppc_elf (link_info.output_bfd))
124 {
125 if (ppc_elf_tls_setup (link_info.output_bfd, &link_info)
126 && !notlsopt)
127 {
128 if (!ppc_elf_tls_optimize (link_info.output_bfd, &link_info))
129 {
130 einfo ("%X%P: TLS problem %E\n");
131 return;
132 }
133 }
134 }
135
136 gld${EMULATION_NAME}_before_allocation ();
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_dataseg_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.ppc476_workaround || params.branch_trampolines)
174 ENABLE_RELAXATION;
175 }
176
177 /* Replaces default zero fill padding in executable sections with
178 "ba 0" instructions. This works around the ppc476 icache bug if we
179 have a function pointer tail call near the end of a page, some
180 small amount of padding, then the function called at the beginning
181 of the next page. If the "ba 0" is ever executed we should hit a
182 segv, so it's almost as good as an illegal instruction (zero). */
183
184 static void
185 no_zero_padding (lang_statement_union_type *l)
186 {
187 if (l->header.type == lang_padding_statement_enum
188 && l->padding_statement.size != 0
189 && l->padding_statement.output_section != NULL
190 && (l->padding_statement.output_section->flags & SEC_CODE) != 0
191 && l->padding_statement.fill->size == 0)
192 {
193 struct _ppc_fill_type
194 {
195 size_t size;
196 unsigned char data[4];
197 };
198 static struct _ppc_fill_type fill_be = { 4, {0x48, 0, 0, 2} };
199 static struct _ppc_fill_type fill_le = { 4, {2, 0, 0, 0x48} };
200
201 if (bfd_big_endian (link_info.output_bfd))
202 l->padding_statement.fill = (struct _fill_type *) &fill_be;
203 else
204 l->padding_statement.fill = (struct _fill_type *) &fill_le;
205 }
206 }
207
208 static void
209 ppc_finish (void)
210 {
211 if (params.ppc476_workaround)
212 lang_for_each_statement (no_zero_padding);
213 finish_default ();
214 }
215
216 EOF
217
218 if grep -q 'ld_elf32_spu_emulation' ldemul-list.h; then
219 fragment <<EOF
220 /* Special handling for embedded SPU executables. */
221 extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *);
222 static bfd_boolean gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *);
223
224 static bfd_boolean
225 ppc_recognized_file (lang_input_statement_type *entry)
226 {
227 if (embedded_spu_file (entry, "-m32"))
228 return TRUE;
229
230 return gld${EMULATION_NAME}_load_symbols (entry);
231 }
232
233 EOF
234 LDEMUL_RECOGNIZED_FILE=ppc_recognized_file
235 fi
236
237 # Define some shell vars to insert bits of code into the standard elf
238 # parse_args and list_options functions.
239 #
240 PARSE_AND_LIST_PROLOGUE=${PARSE_AND_LIST_PROLOGUE}'
241 #define OPTION_NO_TLS_OPT 321
242 #define OPTION_NO_TLS_GET_ADDR_OPT (OPTION_NO_TLS_OPT + 1)
243 #define OPTION_NEW_PLT (OPTION_NO_TLS_GET_ADDR_OPT + 1)
244 #define OPTION_OLD_PLT (OPTION_NEW_PLT + 1)
245 #define OPTION_OLD_GOT (OPTION_OLD_PLT + 1)
246 #define OPTION_STUBSYMS (OPTION_OLD_GOT + 1)
247 #define OPTION_NO_STUBSYMS (OPTION_STUBSYMS + 1)
248 #define OPTION_PPC476_WORKAROUND (OPTION_NO_STUBSYMS + 1)
249 #define OPTION_NO_PPC476_WORKAROUND (OPTION_PPC476_WORKAROUND + 1)
250 '
251
252 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
253 { "emit-stub-syms", no_argument, NULL, OPTION_STUBSYMS },
254 { "no-emit-stub-syms", no_argument, NULL, OPTION_NO_STUBSYMS },
255 { "no-tls-optimize", no_argument, NULL, OPTION_NO_TLS_OPT },
256 { "no-tls-get-addr-optimize", no_argument, NULL, OPTION_NO_TLS_GET_ADDR_OPT },'
257 if test -z "$VXWORKS_BASE_EM_FILE" ; then
258 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
259 { "secure-plt", no_argument, NULL, OPTION_NEW_PLT },
260 { "bss-plt", no_argument, NULL, OPTION_OLD_PLT },
261 { "sdata-got", no_argument, NULL, OPTION_OLD_GOT },'
262 fi
263 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
264 { "ppc476-workaround", optional_argument, NULL, OPTION_PPC476_WORKAROUND },
265 { "no-ppc476-workaround", no_argument, NULL, OPTION_NO_PPC476_WORKAROUND },
266 '
267
268 PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'
269 fprintf (file, _("\
270 --emit-stub-syms Label linker stubs with a symbol.\n\
271 --no-emit-stub-syms Don'\''t label linker stubs with a symbol.\n\
272 --no-tls-optimize Don'\''t try to optimize TLS accesses.\n\
273 --no-tls-get-addr-optimize Don'\''t use a special __tls_get_addr call.\n'
274 if test -z "$VXWORKS_BASE_EM_FILE" ; then
275 PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'\
276 --secure-plt Use new-style PLT if possible.\n\
277 --bss-plt Force old-style BSS PLT.\n\
278 --sdata-got Force GOT location just before .sdata.\n'
279 fi
280 PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'\
281 --ppc476-workaround [=pagesize]\n\
282 Avoid a cache bug on ppc476.\n\
283 --no-ppc476-workaround Disable workaround.\n"
284 ));
285 '
286
287 PARSE_AND_LIST_ARGS_CASES=${PARSE_AND_LIST_ARGS_CASES}'
288 case OPTION_STUBSYMS:
289 params.emit_stub_syms = 1;
290 break;
291
292 case OPTION_NO_STUBSYMS:
293 params.emit_stub_syms = 0;
294 break;
295
296 case OPTION_NO_TLS_OPT:
297 notlsopt = 1;
298 break;
299
300 case OPTION_NO_TLS_GET_ADDR_OPT:
301 params.no_tls_get_addr_opt = 1;
302 break;
303
304 case OPTION_NEW_PLT:
305 params.plt_style = PLT_NEW;
306 break;
307
308 case OPTION_OLD_PLT:
309 params.plt_style = PLT_OLD;
310 break;
311
312 case OPTION_OLD_GOT:
313 old_got = 1;
314 break;
315
316 case OPTION_TRADITIONAL_FORMAT:
317 notlsopt = 1;
318 params.no_tls_get_addr_opt = 1;
319 return FALSE;
320
321 case OPTION_PPC476_WORKAROUND:
322 params.ppc476_workaround = 1;
323 if (optarg != NULL)
324 {
325 char *end;
326 pagesize = strtoul (optarg, &end, 0);
327 if (*end
328 || (pagesize < 4096 && pagesize != 0)
329 || pagesize != (pagesize & -pagesize))
330 einfo (_("%P%F: invalid pagesize `%s'\''\n"), optarg);
331 }
332 break;
333
334 case OPTION_NO_PPC476_WORKAROUND:
335 params.ppc476_workaround = 0;
336 break;
337 '
338
339 # Put these extra ppc32elf routines in ld_${EMULATION_NAME}_emulation
340 #
341 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=ppc_after_open_output
342 if test -z "$VXWORKS_BASE_EM_FILE" ; then
343 LDEMUL_AFTER_OPEN=ppc_after_open
344 fi
345 LDEMUL_BEFORE_ALLOCATION=ppc_before_allocation
346 LDEMUL_FINISH=ppc_finish