]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/emultempl/ppc64elf.em
2004-03-22 Andrew Cagney <cagney@redhat.com>
[thirdparty/binutils-gdb.git] / ld / emultempl / ppc64elf.em
CommitLineData
1f808cd5 1# This shell script emits a C file. -*- C -*-
e0468e59 2# Copyright 2002, 2003 Free Software Foundation, Inc.
1f808cd5
AM
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#
24cat >>e${EMULATION_NAME}.c <<EOF
25
9c1d81c1 26#include "ldctor.h"
805fc799 27#include "libbfd.h"
83490352 28#include "elf-bfd.h"
1f808cd5
AM
29#include "elf64-ppc.h"
30
9c1d81c1
AM
31/* Fake input file for stubs. */
32static lang_input_statement_type *stub_file;
68f8ff14 33static int stub_added = 0;
9c1d81c1
AM
34
35/* Whether we need to call ppc_layout_sections_again. */
1f808cd5
AM
36static int need_laying_out = 0;
37
9c1d81c1
AM
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. */
41static bfd_signed_vma group_size = 1;
42
fac1652d
AM
43/* Whether to add ".foo" entries for each "foo" in a version script. */
44static int dotsyms = 1;
45
e5096e3f
AM
46/* Whether to run tls optimization. */
47static int notlsopt = 0;
48
0b9a4d73
AM
49/* Whether to emit symbols for stubs. */
50static int emit_stub_syms = 0;
51
52static asection *toc_section = 0;
53
9c1d81c1
AM
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
58static void
7d8a3a28 59ppc_create_output_section_statements (void)
9c1d81c1 60{
a015f5ec
AM
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
9c1d81c1
AM
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);
68f8ff14 82 ppc64_elf_init_stub_bfd (stub_file->the_bfd, &link_info);
9c1d81c1
AM
83}
84
e3e942e9 85static void
7d8a3a28 86ppc_after_open (void)
e3e942e9
AM
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}
9c1d81c1 96
836c6af1 97static void
7d8a3a28 98ppc_before_allocation (void)
836c6af1 99{
a015f5ec 100 if (stub_file != NULL)
e0468e59 101 {
a015f5ec 102 if (!ppc64_elf_edit_opd (output_bfd, &link_info))
e5096e3f 103 {
a015f5ec 104 einfo ("%X%P: can not edit opd %E\n");
e5096e3f
AM
105 return;
106 }
107
a015f5ec
AM
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,
7d8a3a28 113 &stat_ptr->head, 0, 0, NULL, TRUE);
a015f5ec
AM
114
115 if (!ppc64_elf_tls_optimize (output_bfd, &link_info))
116 {
117 einfo ("%X%P: TLS problem %E\n");
118 return;
119 }
120
83490352
AM
121 /* We must not cache anything from the preliminary sizing. */
122 elf_tdata (output_bfd)->program_header_size = 0;
a015f5ec
AM
123 lang_reset_memory_regions ();
124 }
e0468e59
AM
125 }
126
836c6af1
AM
127 gld${EMULATION_NAME}_before_allocation ();
128}
129
9c1d81c1
AM
130struct 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
b34976b6 138static bfd_boolean
7d8a3a28 139hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
9c1d81c1
AM
140{
141 lang_statement_union_type *l;
b34976b6 142 bfd_boolean ret;
9c1d81c1
AM
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;
b34976b6 180 return TRUE;
9c1d81c1
AM
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 }
b34976b6 201 return FALSE;
9c1d81c1
AM
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
210static asection *
7d8a3a28 211ppc_add_stub_section (const char *stub_sec_name, asection *input_section)
9c1d81c1
AM
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
68f8ff14 240 stub_added = 1;
9c1d81c1
AM
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
252static void
7d8a3a28 253ppc_layout_sections_again (void)
9c1d81c1
AM
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,
7d8a3a28 264 &stat_ptr->head, 0, 0, NULL, TRUE);
9c1d81c1
AM
265
266 /* Recalculate TOC base. */
267 ldemul_after_allocation ();
268
269 /* Do the assignments again. */
7d8a3a28 270 lang_do_assignments (stat_ptr->head, abs_output_section, NULL, 0);
9c1d81c1
AM
271}
272
1f808cd5
AM
273
274/* Call the back-end function to set TOC base after we have placed all
275 the sections. */
276static void
7d8a3a28 277gld${EMULATION_NAME}_after_allocation (void)
1f808cd5 278{
1049f94e 279 if (!link_info.relocatable)
805fc799 280 _bfd_set_gp_value (output_bfd, ppc64_elf_toc (output_bfd));
1f808cd5
AM
281}
282
9c1d81c1 283
0b9a4d73 284static void
7d8a3a28 285build_toc_list (lang_statement_union_type *statement)
0b9a4d73
AM
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)
7d8a3a28 290 ppc64_elf_next_toc_section (&link_info, statement->input_section.section);
0b9a4d73
AM
291}
292
293
9c1d81c1 294static void
7d8a3a28 295build_section_lists (lang_statement_union_type *statement)
9c1d81c1
AM
296{
297 if (statement->header.type == lang_input_section_enum
805fc799
AM
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)
9c1d81c1 301 {
6bc8652b
AM
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");
9c1d81c1
AM
305 }
306}
307
fac1652d 308
9c1d81c1 309/* Final emulation specific call. */
1f808cd5
AM
310
311static void
7d8a3a28 312gld${EMULATION_NAME}_finish (void)
1f808cd5 313{
1e281515
AM
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
1f808cd5
AM
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
9c1d81c1 322 adding stubs. */
1f808cd5
AM
323 if (bfd_elf${ELFSIZE}_discard_info (output_bfd, &link_info))
324 need_laying_out = 1;
325
836c6af1
AM
326 /* If generating a relocatable output file, then we don't have any
327 stubs. */
1049f94e 328 if (stub_file != NULL && !link_info.relocatable)
1f808cd5 329 {
836c6af1
AM
330 int ret = ppc64_elf_setup_section_lists (output_bfd, &link_info);
331 if (ret != 0)
9c1d81c1 332 {
836c6af1
AM
333 if (ret < 0)
334 {
335 einfo ("%X%P: can not size stub section: %E\n");
336 return;
337 }
9c1d81c1 338
68f8ff14 339 toc_section = bfd_get_section_by_name (output_bfd, ".got");
0b9a4d73
AM
340 if (toc_section != NULL)
341 lang_for_each_statement (build_toc_list);
342
343 ppc64_elf_reinit_toc (output_bfd, &link_info);
344
836c6af1 345 lang_for_each_statement (build_section_lists);
9c1d81c1 346
836c6af1
AM
347 /* Call into the BFD backend to do the real work. */
348 if (!ppc64_elf_size_stubs (output_bfd,
836c6af1
AM
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 }
1f808cd5 357 }
9c1d81c1 358 }
1f808cd5 359
9c1d81c1
AM
360 if (need_laying_out)
361 ppc_layout_sections_again ();
1f808cd5 362
68f8ff14 363 if (stub_added)
9c1d81c1 364 {
bda6c678
AM
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))
9c1d81c1 370 einfo ("%X%P: can not build stubs: %E\n");
bda6c678
AM
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);
9c1d81c1
AM
381 }
382}
1f808cd5 383
76dc39fe 384
fac1652d
AM
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
7d8a3a28
AM
403static struct bfd_elf_version_expr *
404gld${EMULATION_NAME}_new_vers_pattern (struct bfd_elf_version_expr *entry)
fac1652d
AM
405{
406 struct bfd_elf_version_expr *dot_entry;
fac1652d
AM
407 unsigned int len;
408 char *dot_pat;
409
108ba305 410 if (!dotsyms || entry->pattern[0] == '*' || entry->pattern[0] == '.')
fac1652d
AM
411 return entry;
412
7d8a3a28 413 dot_entry = xmalloc (sizeof *dot_entry);
108ba305 414 *dot_entry = *entry;
fac1652d
AM
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;
fac1652d
AM
421 return dot_entry;
422}
423
424
9c1d81c1
AM
425/* Avoid processing the fake stub_file in vercheck, stat_needed and
426 check_needed routines. */
1f808cd5 427
7d8a3a28 428static void (*real_func) (lang_input_statement_type *);
1f808cd5 429
7d8a3a28 430static void ppc_for_each_input_file_wrapper (lang_input_statement_type *l)
9c1d81c1
AM
431{
432 if (l != stub_file)
433 (*real_func) (l);
1f808cd5 434}
9c1d81c1
AM
435
436static void
7d8a3a28 437ppc_lang_for_each_input_file (void (*func) (lang_input_statement_type *))
9c1d81c1
AM
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
1f808cd5
AM
445EOF
446
9c1d81c1
AM
447# Define some shell vars to insert bits of code into the standard elf
448# parse_args and list_options functions.
449#
450PARSE_AND_LIST_PROLOGUE='
451#define OPTION_STUBGROUP_SIZE 301
0b9a4d73
AM
452#define OPTION_STUBSYMS (OPTION_STUBGROUP_SIZE + 1)
453#define OPTION_DOTSYMS (OPTION_STUBSYMS + 1)
fac1652d 454#define OPTION_NO_DOTSYMS (OPTION_DOTSYMS + 1)
e5096e3f 455#define OPTION_NO_TLS_OPT (OPTION_NO_DOTSYMS + 1)
9c1d81c1
AM
456'
457
9c1d81c1
AM
458PARSE_AND_LIST_LONGOPTS='
459 { "stub-group-size", required_argument, NULL, OPTION_STUBGROUP_SIZE },
0b9a4d73 460 { "emit-stub-syms", no_argument, NULL, OPTION_STUBSYMS },
fac1652d
AM
461 { "dotsyms", no_argument, NULL, OPTION_DOTSYMS },
462 { "no-dotsyms", no_argument, NULL, OPTION_NO_DOTSYMS },
e5096e3f 463 { "no-tls-optimize", no_argument, NULL, OPTION_NO_TLS_OPT },
9c1d81c1
AM
464'
465
466PARSE_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 ));
fac1652d 476 fprintf (file, _("\
0b9a4d73
AM
477 --emit-stub-syms Label linker stubs with a symbol.\n"
478 ));
479 fprintf (file, _("\
fac1652d
AM
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 ));
e5096e3f
AM
488 fprintf (file, _("\
489 --no-tls-optimize Don'\''t try to optimize TLS accesses.\n"
490 ));
9c1d81c1
AM
491'
492
493PARSE_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;
fac1652d 502
0b9a4d73
AM
503 case OPTION_STUBSYMS:
504 emit_stub_syms = 1;
505 break;
506
fac1652d 507 case OPTION_DOTSYMS:
e5096e3f 508 dotsyms = 1;
fac1652d
AM
509 break;
510
511 case OPTION_NO_DOTSYMS:
e5096e3f
AM
512 dotsyms = 0;
513 break;
514
515 case OPTION_NO_TLS_OPT:
516 notlsopt = 1;
fac1652d 517 break;
9c1d81c1
AM
518'
519
1f808cd5
AM
520# Put these extra ppc64elf routines in ld_${EMULATION_NAME}_emulation
521#
e3e942e9 522LDEMUL_AFTER_OPEN=ppc_after_open
836c6af1 523LDEMUL_BEFORE_ALLOCATION=ppc_before_allocation
1f808cd5
AM
524LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
525LDEMUL_FINISH=gld${EMULATION_NAME}_finish
9c1d81c1 526LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=ppc_create_output_section_statements
fac1652d 527LDEMUL_NEW_VERS_PATTERN=gld${EMULATION_NAME}_new_vers_pattern