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