* emultempl/ppc64elf.em (ppc_before_allocation): Clear cached
[binutils-gdb.git] / ld / emultempl / ppc64elf.em
1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2002, 2003 Free Software Foundation, Inc.
3 #
4 # This file is part of GLD, the Gnu Linker.
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #
20
21 # This file is sourced from elf32.em, and defines extra powerpc64-elf
22 # specific routines.
23 #
24 cat >>e${EMULATION_NAME}.c <<EOF
25
26 #include "ldctor.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29 #include "elf64-ppc.h"
30
31 /* Fake input file for stubs. */
32 static lang_input_statement_type *stub_file;
33 static int stub_added = 0;
34
35 /* Whether we need to call ppc_layout_sections_again. */
36 static int need_laying_out = 0;
37
38 /* Maximum size of a group of input sections that can be handled by
39 one stub section. A value of +/-1 indicates the bfd back-end
40 should use a suitable default size. */
41 static bfd_signed_vma group_size = 1;
42
43 /* Whether to add ".foo" entries for each "foo" in a version script. */
44 static int dotsyms = 1;
45
46 /* Whether to run tls optimization. */
47 static int notlsopt = 0;
48
49 /* Whether to emit symbols for stubs. */
50 static int emit_stub_syms = 0;
51
52 static asection *toc_section = 0;
53
54
55 /* This is called before the input files are opened. We create a new
56 fake input file to hold the stub sections. */
57
58 static void
59 ppc_create_output_section_statements (void)
60 {
61 extern const bfd_target bfd_elf64_powerpc_vec;
62 extern const bfd_target bfd_elf64_powerpcle_vec;
63
64 if (link_info.hash->creator != &bfd_elf64_powerpc_vec
65 && link_info.hash->creator != &bfd_elf64_powerpcle_vec)
66 return;
67
68 stub_file = lang_add_input_file ("linker stubs",
69 lang_input_file_is_fake_enum,
70 NULL);
71 stub_file->the_bfd = bfd_create ("linker stubs", output_bfd);
72 if (stub_file->the_bfd == NULL
73 || !bfd_set_arch_mach (stub_file->the_bfd,
74 bfd_get_arch (output_bfd),
75 bfd_get_mach (output_bfd)))
76 {
77 einfo ("%X%P: can not create BFD %E\n");
78 return;
79 }
80
81 ldlang_add_file (stub_file);
82 ppc64_elf_init_stub_bfd (stub_file->the_bfd, &link_info);
83 }
84
85 static void
86 ppc_after_open (void)
87 {
88 if (!ppc64_elf_mark_entry_syms (&link_info))
89 {
90 einfo ("%X%P: can not mark entry symbols %E\n");
91 return;
92 }
93
94 gld${EMULATION_NAME}_after_open ();
95 }
96
97 static void
98 ppc_before_allocation (void)
99 {
100 if (stub_file != NULL)
101 {
102 if (!ppc64_elf_edit_opd (output_bfd, &link_info))
103 {
104 einfo ("%X%P: can not edit opd %E\n");
105 return;
106 }
107
108 if (ppc64_elf_tls_setup (output_bfd, &link_info) && !notlsopt)
109 {
110 /* Size the sections. This is premature, but we want to know the
111 TLS segment layout so that certain optimizations can be done. */
112 lang_size_sections (stat_ptr->head, abs_output_section,
113 &stat_ptr->head, 0, 0, NULL, TRUE);
114
115 if (!ppc64_elf_tls_optimize (output_bfd, &link_info))
116 {
117 einfo ("%X%P: TLS problem %E\n");
118 return;
119 }
120
121 /* We must not cache anything from the preliminary sizing. */
122 elf_tdata (output_bfd)->program_header_size = 0;
123 lang_reset_memory_regions ();
124 }
125 }
126
127 gld${EMULATION_NAME}_before_allocation ();
128 }
129
130 struct hook_stub_info
131 {
132 lang_statement_list_type add;
133 asection *input_section;
134 };
135
136 /* Traverse the linker tree to find the spot where the stub goes. */
137
138 static bfd_boolean
139 hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
140 {
141 lang_statement_union_type *l;
142 bfd_boolean ret;
143
144 for (; (l = *lp) != NULL; lp = &l->header.next)
145 {
146 switch (l->header.type)
147 {
148 case lang_constructors_statement_enum:
149 ret = hook_in_stub (info, &constructor_list.head);
150 if (ret)
151 return ret;
152 break;
153
154 case lang_output_section_statement_enum:
155 ret = hook_in_stub (info,
156 &l->output_section_statement.children.head);
157 if (ret)
158 return ret;
159 break;
160
161 case lang_wild_statement_enum:
162 ret = hook_in_stub (info, &l->wild_statement.children.head);
163 if (ret)
164 return ret;
165 break;
166
167 case lang_group_statement_enum:
168 ret = hook_in_stub (info, &l->group_statement.children.head);
169 if (ret)
170 return ret;
171 break;
172
173 case lang_input_section_enum:
174 if (l->input_section.section == info->input_section)
175 {
176 /* We've found our section. Insert the stub immediately
177 before its associated input section. */
178 *lp = info->add.head;
179 *(info->add.tail) = l;
180 return TRUE;
181 }
182 break;
183
184 case lang_data_statement_enum:
185 case lang_reloc_statement_enum:
186 case lang_object_symbols_statement_enum:
187 case lang_output_statement_enum:
188 case lang_target_statement_enum:
189 case lang_input_statement_enum:
190 case lang_assignment_statement_enum:
191 case lang_padding_statement_enum:
192 case lang_address_statement_enum:
193 case lang_fill_statement_enum:
194 break;
195
196 default:
197 FAIL ();
198 break;
199 }
200 }
201 return FALSE;
202 }
203
204
205 /* Call-back for ppc64_elf_size_stubs. */
206
207 /* Create a new stub section, and arrange for it to be linked
208 immediately before INPUT_SECTION. */
209
210 static asection *
211 ppc_add_stub_section (const char *stub_sec_name, asection *input_section)
212 {
213 asection *stub_sec;
214 flagword flags;
215 asection *output_section;
216 const char *secname;
217 lang_output_section_statement_type *os;
218 struct hook_stub_info info;
219
220 stub_sec = bfd_make_section_anyway (stub_file->the_bfd, stub_sec_name);
221 if (stub_sec == NULL)
222 goto err_ret;
223
224 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
225 | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
226 if (!bfd_set_section_flags (stub_file->the_bfd, stub_sec, flags))
227 goto err_ret;
228
229 output_section = input_section->output_section;
230 secname = bfd_get_section_name (output_section->owner, output_section);
231 os = lang_output_section_find (secname);
232
233 info.input_section = input_section;
234 lang_list_init (&info.add);
235 lang_add_section (&info.add, stub_sec, os, stub_file);
236
237 if (info.add.head == NULL)
238 goto err_ret;
239
240 stub_added = 1;
241 if (hook_in_stub (&info, &os->children.head))
242 return stub_sec;
243
244 err_ret:
245 einfo ("%X%P: can not make stub section: %E\n");
246 return NULL;
247 }
248
249
250 /* Another call-back for ppc64_elf_size_stubs. */
251
252 static void
253 ppc_layout_sections_again (void)
254 {
255 /* If we have changed sizes of the stub sections, then we need
256 to recalculate all the section offsets. This may mean we need to
257 add even more stubs. */
258 need_laying_out = 0;
259
260 lang_reset_memory_regions ();
261
262 /* Resize the sections. */
263 lang_size_sections (stat_ptr->head, abs_output_section,
264 &stat_ptr->head, 0, 0, NULL, TRUE);
265
266 /* Recalculate TOC base. */
267 ldemul_after_allocation ();
268
269 /* Do the assignments again. */
270 lang_do_assignments (stat_ptr->head, abs_output_section, NULL, 0);
271 }
272
273
274 /* Call the back-end function to set TOC base after we have placed all
275 the sections. */
276 static void
277 gld${EMULATION_NAME}_after_allocation (void)
278 {
279 if (!link_info.relocatable)
280 _bfd_set_gp_value (output_bfd, ppc64_elf_toc (output_bfd));
281 }
282
283
284 static void
285 build_toc_list (lang_statement_union_type *statement)
286 {
287 if (statement->header.type == lang_input_section_enum
288 && !statement->input_section.ifile->just_syms_flag
289 && statement->input_section.section->output_section == toc_section)
290 ppc64_elf_next_toc_section (&link_info, statement->input_section.section);
291 }
292
293
294 static void
295 build_section_lists (lang_statement_union_type *statement)
296 {
297 if (statement->header.type == lang_input_section_enum
298 && !statement->input_section.ifile->just_syms_flag
299 && statement->input_section.section->output_section != NULL
300 && statement->input_section.section->output_section->owner == output_bfd)
301 {
302 if (!ppc64_elf_next_input_section (&link_info,
303 statement->input_section.section))
304 einfo ("%X%P: can not size stub section: %E\n");
305 }
306 }
307
308
309 /* Final emulation specific call. */
310
311 static void
312 gld${EMULATION_NAME}_finish (void)
313 {
314 /* e_entry on PowerPC64 points to the function descriptor for
315 _start. If _start is missing, default to the first function
316 descriptor in the .opd section. */
317 entry_section = ".opd";
318
319 /* bfd_elf64_discard_info just plays with debugging sections,
320 ie. doesn't affect any code, so we can delay resizing the
321 sections. It's likely we'll resize everything in the process of
322 adding stubs. */
323 if (bfd_elf${ELFSIZE}_discard_info (output_bfd, &link_info))
324 need_laying_out = 1;
325
326 /* If generating a relocatable output file, then we don't have any
327 stubs. */
328 if (stub_file != NULL && !link_info.relocatable)
329 {
330 int ret = ppc64_elf_setup_section_lists (output_bfd, &link_info);
331 if (ret != 0)
332 {
333 if (ret < 0)
334 {
335 einfo ("%X%P: can not size stub section: %E\n");
336 return;
337 }
338
339 toc_section = bfd_get_section_by_name (output_bfd, ".got");
340 if (toc_section != NULL)
341 lang_for_each_statement (build_toc_list);
342
343 ppc64_elf_reinit_toc (output_bfd, &link_info);
344
345 lang_for_each_statement (build_section_lists);
346
347 /* Call into the BFD backend to do the real work. */
348 if (!ppc64_elf_size_stubs (output_bfd,
349 &link_info,
350 group_size,
351 &ppc_add_stub_section,
352 &ppc_layout_sections_again))
353 {
354 einfo ("%X%P: can not size stub section: %E\n");
355 return;
356 }
357 }
358 }
359
360 if (need_laying_out)
361 ppc_layout_sections_again ();
362
363 if (stub_added)
364 {
365 char *msg = NULL;
366 char *line, *endline;
367
368 if (!ppc64_elf_build_stubs (emit_stub_syms, &link_info,
369 config.stats ? &msg : NULL))
370 einfo ("%X%P: can not build stubs: %E\n");
371
372 for (line = msg; line != NULL; line = endline)
373 {
374 endline = strchr (line, '\n');
375 if (endline != NULL)
376 *endline++ = '\0';
377 fprintf (stderr, "%s: %s\n", program_name, line);
378 }
379 if (msg != NULL)
380 free (msg);
381 }
382 }
383
384
385 /* Add a pattern matching ".foo" for every "foo" in a version script.
386
387 The reason for doing this is that many shared library version
388 scripts export a selected set of functions or data symbols, forcing
389 others local. eg.
390
391 . VERS_1 {
392 . global:
393 . this; that; some; thing;
394 . local:
395 . *;
396 . };
397
398 To make the above work for PowerPC64, we need to export ".this",
399 ".that" and so on, otherwise only the function descriptor syms are
400 exported. Lack of an exported function code sym may cause a
401 definition to be pulled in from a static library. */
402
403 static struct bfd_elf_version_expr *
404 gld${EMULATION_NAME}_new_vers_pattern (struct bfd_elf_version_expr *entry)
405 {
406 struct bfd_elf_version_expr *dot_entry;
407 unsigned int len;
408 char *dot_pat;
409
410 if (!dotsyms || entry->pattern[0] == '*' || entry->pattern[0] == '.')
411 return entry;
412
413 dot_entry = xmalloc (sizeof *dot_entry);
414 *dot_entry = *entry;
415 dot_entry->next = entry;
416 len = strlen (entry->pattern) + 2;
417 dot_pat = xmalloc (len);
418 dot_pat[0] = '.';
419 memcpy (dot_pat + 1, entry->pattern, len - 1);
420 dot_entry->pattern = dot_pat;
421 return dot_entry;
422 }
423
424
425 /* Avoid processing the fake stub_file in vercheck, stat_needed and
426 check_needed routines. */
427
428 static void (*real_func) (lang_input_statement_type *);
429
430 static void ppc_for_each_input_file_wrapper (lang_input_statement_type *l)
431 {
432 if (l != stub_file)
433 (*real_func) (l);
434 }
435
436 static void
437 ppc_lang_for_each_input_file (void (*func) (lang_input_statement_type *))
438 {
439 real_func = func;
440 lang_for_each_input_file (&ppc_for_each_input_file_wrapper);
441 }
442
443 #define lang_for_each_input_file ppc_lang_for_each_input_file
444
445 EOF
446
447 # Define some shell vars to insert bits of code into the standard elf
448 # parse_args and list_options functions.
449 #
450 PARSE_AND_LIST_PROLOGUE='
451 #define OPTION_STUBGROUP_SIZE 301
452 #define OPTION_STUBSYMS (OPTION_STUBGROUP_SIZE + 1)
453 #define OPTION_DOTSYMS (OPTION_STUBSYMS + 1)
454 #define OPTION_NO_DOTSYMS (OPTION_DOTSYMS + 1)
455 #define OPTION_NO_TLS_OPT (OPTION_NO_DOTSYMS + 1)
456 '
457
458 PARSE_AND_LIST_LONGOPTS='
459 { "stub-group-size", required_argument, NULL, OPTION_STUBGROUP_SIZE },
460 { "emit-stub-syms", no_argument, NULL, OPTION_STUBSYMS },
461 { "dotsyms", no_argument, NULL, OPTION_DOTSYMS },
462 { "no-dotsyms", no_argument, NULL, OPTION_NO_DOTSYMS },
463 { "no-tls-optimize", no_argument, NULL, OPTION_NO_TLS_OPT },
464 '
465
466 PARSE_AND_LIST_OPTIONS='
467 fprintf (file, _("\
468 --stub-group-size=N Maximum size of a group of input sections that can be\n\
469 handled by one stub section. A negative value\n\
470 locates all stubs before their branches (with a\n\
471 group size of -N), while a positive value allows\n\
472 two groups of input sections, one before, and one\n\
473 after each stub section. Values of +/-1 indicate\n\
474 the linker should choose suitable defaults.\n"
475 ));
476 fprintf (file, _("\
477 --emit-stub-syms Label linker stubs with a symbol.\n"
478 ));
479 fprintf (file, _("\
480 --dotsyms For every version pattern \"foo\" in a version script,\n\
481 add \".foo\" so that function code symbols are\n\
482 treated the same as function descriptor symbols.\n\
483 Defaults to on.\n"
484 ));
485 fprintf (file, _("\
486 --no-dotsyms Don'\''t do anything special in version scripts.\n"
487 ));
488 fprintf (file, _("\
489 --no-tls-optimize Don'\''t try to optimize TLS accesses.\n"
490 ));
491 '
492
493 PARSE_AND_LIST_ARGS_CASES='
494 case OPTION_STUBGROUP_SIZE:
495 {
496 const char *end;
497 group_size = bfd_scan_vma (optarg, &end, 0);
498 if (*end)
499 einfo (_("%P%F: invalid number `%s'\''\n"), optarg);
500 }
501 break;
502
503 case OPTION_STUBSYMS:
504 emit_stub_syms = 1;
505 break;
506
507 case OPTION_DOTSYMS:
508 dotsyms = 1;
509 break;
510
511 case OPTION_NO_DOTSYMS:
512 dotsyms = 0;
513 break;
514
515 case OPTION_NO_TLS_OPT:
516 notlsopt = 1;
517 break;
518 '
519
520 # Put these extra ppc64elf routines in ld_${EMULATION_NAME}_emulation
521 #
522 LDEMUL_AFTER_OPEN=ppc_after_open
523 LDEMUL_BEFORE_ALLOCATION=ppc_before_allocation
524 LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
525 LDEMUL_FINISH=gld${EMULATION_NAME}_finish
526 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=ppc_create_output_section_statements
527 LDEMUL_NEW_VERS_PATTERN=gld${EMULATION_NAME}_new_vers_pattern