]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/emultempl/ppc32elf.em
ppc476 workaround for ld -r fixes
[thirdparty/binutils-gdb.git] / ld / emultempl / ppc32elf.em
1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2003, 2005, 2007, 2008, 2009, 2010, 2011, 2012
3 # Free Software Foundation, Inc.
4 #
5 # This file is part of the GNU Binutils.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 # MA 02110-1301, USA.
21 #
22
23 # This file is sourced from elf32.em, and defines extra powerpc32-elf
24 # specific routines.
25 #
26 fragment <<EOF
27
28 #include "libbfd.h"
29 #include "elf32-ppc.h"
30 #include "ldlex.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 static void
58 ppc_after_open (void)
59 {
60 if (is_ppc_elf (link_info.output_bfd))
61 {
62 int new_plt;
63 int keep_new;
64 unsigned int num_plt;
65 unsigned int num_got;
66 lang_output_section_statement_type *os;
67 lang_output_section_statement_type *plt_os[2];
68 lang_output_section_statement_type *got_os[2];
69
70 new_plt = ppc_elf_select_plt_layout (link_info.output_bfd, &link_info);
71 if (new_plt < 0)
72 einfo ("%X%P: select_plt_layout problem %E\n");
73
74 num_got = 0;
75 num_plt = 0;
76 for (os = &lang_output_section_statement.head->output_section_statement;
77 os != NULL;
78 os = os->next)
79 {
80 if (os->constraint == SPECIAL && strcmp (os->name, ".plt") == 0)
81 {
82 if (num_plt < 2)
83 plt_os[num_plt] = os;
84 ++num_plt;
85 }
86 if (os->constraint == SPECIAL && strcmp (os->name, ".got") == 0)
87 {
88 if (num_got < 2)
89 got_os[num_got] = os;
90 ++num_got;
91 }
92 }
93
94 keep_new = new_plt == 1 ? 0 : -1;
95 if (num_plt == 2)
96 {
97 plt_os[0]->constraint = keep_new;
98 plt_os[1]->constraint = ~keep_new;
99 }
100 if (num_got == 2)
101 {
102 if (old_got)
103 keep_new = -1;
104 got_os[0]->constraint = keep_new;
105 got_os[1]->constraint = ~keep_new;
106 }
107 }
108
109 gld${EMULATION_NAME}_after_open ();
110 }
111
112 static void
113 ppc_before_allocation (void)
114 {
115 if (is_ppc_elf (link_info.output_bfd))
116 {
117 if (ppc_elf_tls_setup (link_info.output_bfd, &link_info)
118 && !notlsopt)
119 {
120 if (!ppc_elf_tls_optimize (link_info.output_bfd, &link_info))
121 {
122 einfo ("%X%P: TLS problem %E\n");
123 return;
124 }
125 }
126 }
127
128 gld${EMULATION_NAME}_before_allocation ();
129
130 if (RELAXATION_ENABLED)
131 params.branch_trampolines = 1;
132
133 /* Turn on relaxation if executable sections have addresses that
134 might make branches overflow. */
135 else if (!RELAXATION_DISABLED_BY_USER)
136 {
137 bfd_vma low = (bfd_vma) -1;
138 bfd_vma high = 0;
139 asection *o;
140
141 /* Run lang_size_sections (if not already done). */
142 if (expld.phase != lang_mark_phase_enum)
143 {
144 expld.phase = lang_mark_phase_enum;
145 expld.dataseg.phase = exp_dataseg_none;
146 one_lang_size_sections_pass (NULL, FALSE);
147 lang_reset_memory_regions ();
148 }
149
150 for (o = link_info.output_bfd->sections; o != NULL; o = o->next)
151 {
152 if ((o->flags & (SEC_ALLOC | SEC_CODE)) != (SEC_ALLOC | SEC_CODE))
153 continue;
154 if (o->rawsize == 0)
155 continue;
156 if (low > o->vma)
157 low = o->vma;
158 if (high < o->vma + o->rawsize - 1)
159 high = o->vma + o->rawsize - 1;
160 }
161 if (high > low && high - low > (1 << 25) - 1)
162 params.branch_trampolines = 1;
163 }
164
165 if (params.ppc476_workaround || params.branch_trampolines)
166 ENABLE_RELAXATION;
167 }
168
169 EOF
170
171 if grep -q 'ld_elf32_spu_emulation' ldemul-list.h; then
172 fragment <<EOF
173 /* Special handling for embedded SPU executables. */
174 extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *);
175 static bfd_boolean gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *);
176
177 static bfd_boolean
178 ppc_recognized_file (lang_input_statement_type *entry)
179 {
180 if (embedded_spu_file (entry, "-m32"))
181 return TRUE;
182
183 return gld${EMULATION_NAME}_load_symbols (entry);
184 }
185
186 EOF
187 LDEMUL_RECOGNIZED_FILE=ppc_recognized_file
188 fi
189
190 # Define some shell vars to insert bits of code into the standard elf
191 # parse_args and list_options functions.
192 #
193 PARSE_AND_LIST_PROLOGUE=${PARSE_AND_LIST_PROLOGUE}'
194 #define OPTION_NO_TLS_OPT 321
195 #define OPTION_NO_TLS_GET_ADDR_OPT (OPTION_NO_TLS_OPT + 1)
196 #define OPTION_NEW_PLT (OPTION_NO_TLS_GET_ADDR_OPT + 1)
197 #define OPTION_OLD_PLT (OPTION_NEW_PLT + 1)
198 #define OPTION_OLD_GOT (OPTION_OLD_PLT + 1)
199 #define OPTION_STUBSYMS (OPTION_OLD_GOT + 1)
200 #define OPTION_NO_STUBSYMS (OPTION_STUBSYMS + 1)
201 #define OPTION_PPC476_WORKAROUND (OPTION_NO_STUBSYMS + 1)
202 #define OPTION_NO_PPC476_WORKAROUND (OPTION_PPC476_WORKAROUND + 1)
203 '
204
205 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
206 { "emit-stub-syms", no_argument, NULL, OPTION_STUBSYMS },
207 { "no-emit-stub-syms", no_argument, NULL, OPTION_NO_STUBSYMS },
208 { "no-tls-optimize", no_argument, NULL, OPTION_NO_TLS_OPT },
209 { "no-tls-get-addr-optimize", no_argument, NULL, OPTION_NO_TLS_GET_ADDR_OPT },
210 { "secure-plt", no_argument, NULL, OPTION_NEW_PLT },
211 { "bss-plt", no_argument, NULL, OPTION_OLD_PLT },
212 { "sdata-got", no_argument, NULL, OPTION_OLD_GOT },
213 { "ppc476-workaround", optional_argument, NULL, OPTION_PPC476_WORKAROUND },
214 { "no-ppc476-workaround", no_argument, NULL, OPTION_NO_PPC476_WORKAROUND },
215 '
216
217 PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'
218 fprintf (file, _("\
219 --emit-stub-syms Label linker stubs with a symbol.\n\
220 --no-emit-stub-syms Don'\''t label linker stubs with a symbol.\n\
221 --no-tls-optimize Don'\''t try to optimize TLS accesses.\n\
222 --no-tls-get-addr-optimize Don'\''t use a special __tls_get_addr call.\n\
223 --secure-plt Use new-style PLT if possible.\n\
224 --bss-plt Force old-style BSS PLT.\n\
225 --sdata-got Force GOT location just before .sdata.\n\
226 --ppc476-workaround [=pagesize]\n\
227 Avoid a cache bug on ppc476.\n\
228 --no-ppc476-workaround Disable workaround.\n"
229 ));
230 '
231
232 PARSE_AND_LIST_ARGS_CASES=${PARSE_AND_LIST_ARGS_CASES}'
233 case OPTION_STUBSYMS:
234 params.emit_stub_syms = 1;
235 break;
236
237 case OPTION_NO_STUBSYMS:
238 params.emit_stub_syms = 0;
239 break;
240
241 case OPTION_NO_TLS_OPT:
242 notlsopt = 1;
243 break;
244
245 case OPTION_NO_TLS_GET_ADDR_OPT:
246 params.no_tls_get_addr_opt = 1;
247 break;
248
249 case OPTION_NEW_PLT:
250 params.plt_style = PLT_NEW;
251 break;
252
253 case OPTION_OLD_PLT:
254 params.plt_style = PLT_OLD;
255 break;
256
257 case OPTION_OLD_GOT:
258 old_got = 1;
259 break;
260
261 case OPTION_TRADITIONAL_FORMAT:
262 notlsopt = 1;
263 params.no_tls_get_addr_opt = 1;
264 return FALSE;
265
266 case OPTION_PPC476_WORKAROUND:
267 params.ppc476_workaround = 1;
268 if (optarg != NULL)
269 {
270 char *end;
271 pagesize = strtoul (optarg, &end, 0);
272 if (*end
273 || (pagesize < 4096 && pagesize != 0)
274 || pagesize != (pagesize & -pagesize))
275 einfo (_("%P%F: invalid pagesize `%s'\''\n"), optarg);
276 }
277 break;
278
279 case OPTION_NO_PPC476_WORKAROUND:
280 params.ppc476_workaround = 0;
281 break;
282 '
283
284 # Put these extra ppc32elf routines in ld_${EMULATION_NAME}_emulation
285 #
286 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=ppc_after_open_output
287 LDEMUL_AFTER_OPEN=ppc_after_open
288 LDEMUL_BEFORE_ALLOCATION=ppc_before_allocation