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