1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 # This file is part of the GNU Binutils.
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 3 of the License, or
9 # (at your option) any later version.
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.
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,
22 # This file is sourced from elf32.em, and defines extra spu specific
27 #include "elf32-spu.h"
29 static void spu_place_special_section (asection *, asection *, const char *);
30 static bfd_size_type spu_elf_load_ovl_mgr (void);
31 static FILE *spu_elf_open_overlay_script (void);
32 static void spu_elf_relink (void);
34 static struct spu_elf_params params =
36 &spu_place_special_section,
37 &spu_elf_load_ovl_mgr,
38 &spu_elf_open_overlay_script,
40 0, ovly_normal, 0, 0, 0, 0,
45 static char *auto_overlay_file = 0;
49 static const char ovl_mgr[] = {
52 if ! cat ${srcdir}/emultempl/spu_ovl.o_c >> e${EMULATION_NAME}.c
54 echo >&2 "Missing ${srcdir}/emultempl/spu_ovl.o_c"
55 echo >&2 "You must build gas/as-new with --target=spu to build spu_ovl.o"
62 static const struct _ovl_stream ovl_mgr_stream = {
64 ovl_mgr + sizeof (ovl_mgr)
71 extern const bfd_target bfd_elf32_spu_vec;
73 return link_info.output_bfd->xvec == &bfd_elf32_spu_vec;
76 /* Create our note section. */
83 /* Pass params to backend. */
84 if ((params.auto_overlay & AUTO_OVERLAY) == 0)
85 params.auto_overlay = 0;
86 params.emit_stub_syms |= link_info.emitrelocations;
87 spu_elf_setup (&link_info, ¶ms);
89 if (!link_info.relocatable
90 && link_info.input_bfds != NULL
91 && !spu_elf_create_sections (&link_info))
92 einfo ("%X%P: can not create note section: %E\n");
95 gld${EMULATION_NAME}_after_open ();
98 /* If O is NULL, add section S at the end of output section OUTPUT_NAME.
99 If O is not NULL, add section S at the beginning of output section O.
101 Really, we should be duplicating ldlang.c map_input_to_output_sections
102 logic here, ie. using the linker script to find where the section
103 goes. That's rather a lot of code, and we don't want to run
104 map_input_to_output_sections again because most sections are already
105 mapped. So cheat, and put the section in a fixed place, ignoring any
106 attempt via a linker script to put .stub, .ovtab, and built-in
107 overlay manager code somewhere else. */
110 spu_place_special_section (asection *s, asection *o, const char *output_name)
112 lang_output_section_statement_type *os;
115 output_name = o->name;
116 os = lang_output_section_find (output_name);
118 gld${EMULATION_NAME}_place_orphan (s, output_name, 0);
119 else if (o != NULL && os->children.head != NULL)
121 lang_statement_list_type add;
123 lang_list_init (&add);
124 lang_add_section (&add, s, os);
125 *add.tail = os->children.head;
126 os->children.head = add.head;
129 lang_add_section (&os->children, s, os);
131 s->output_section->size += s->size;
134 /* Load built-in overlay manager. */
137 spu_elf_load_ovl_mgr (void)
139 struct elf_link_hash_entry *h;
140 bfd_size_type total = 0;
142 h = elf_link_hash_lookup (elf_hash_table (&link_info),
143 "__ovly_load", FALSE, FALSE, FALSE);
146 && (h->root.type == bfd_link_hash_defined
147 || h->root.type == bfd_link_hash_defweak)
150 /* User supplied __ovly_load. */
152 else if (ovl_mgr_stream.start == ovl_mgr_stream.end)
153 einfo ("%F%P: no built-in overlay manager\n");
156 lang_input_statement_type *ovl_is;
158 ovl_is = lang_add_input_file ("builtin ovl_mgr",
159 lang_input_file_is_file_enum,
162 if (!spu_elf_open_builtin_lib (&ovl_is->the_bfd, &ovl_mgr_stream))
163 einfo ("%X%P: can not open built-in overlay manager: %E\n");
168 if (!load_symbols (ovl_is, NULL))
169 einfo ("%X%P: can not load built-in overlay manager: %E\n");
171 /* Map overlay manager sections to output sections. */
172 for (in = ovl_is->the_bfd->sections; in != NULL; in = in->next)
173 if ((in->flags & (SEC_ALLOC | SEC_LOAD))
174 == (SEC_ALLOC | SEC_LOAD))
177 spu_place_special_section (in, NULL, ".text");
184 /* Go find if we need to do anything special for overlays. */
187 spu_before_allocation (void)
190 && !link_info.relocatable
191 && params.ovly_flavour != ovly_none)
193 /* Size the sections. This is premature, but we need to know the
194 rough layout so that overlays can be found. */
195 expld.phase = lang_mark_phase_enum;
196 expld.dataseg.phase = exp_dataseg_none;
197 one_lang_size_sections_pass (NULL, TRUE);
199 /* Find overlays by inspecting section vmas. */
200 if (spu_elf_find_overlays (&link_info))
203 lang_output_section_statement_type *os;
205 if (params.auto_overlay != 0)
207 einfo ("%P: --auto-overlay ignored with user overlay script\n");
208 params.auto_overlay = 0;
211 /* Ensure alignment of overlay sections is sufficient. */
212 for (os = &lang_output_section_statement.head->output_section_statement;
215 if (os->bfd_section != NULL
216 && spu_elf_section_data (os->bfd_section) != NULL
217 && spu_elf_section_data (os->bfd_section)->u.o.ovl_index != 0)
219 if (os->bfd_section->alignment_power < 4)
220 os->bfd_section->alignment_power = 4;
222 /* Also ensure size rounds up. */
223 os->block_value = 16;
226 ret = spu_elf_size_stubs (&link_info);
228 einfo ("%X%P: can not size overlay stubs: %E\n");
230 spu_elf_load_ovl_mgr ();
233 /* We must not cache anything from the preliminary sizing. */
234 lang_reset_memory_regions ();
237 gld${EMULATION_NAME}_before_allocation ();
245 static struct tflist *tmp_file_list;
247 static void clean_tmp (void)
249 for (; tmp_file_list != NULL; tmp_file_list = tmp_file_list->next)
250 unlink (tmp_file_list->name);
254 new_tmp_file (char **fname)
259 if (tmp_file_list == NULL)
261 tf = xmalloc (sizeof (*tf));
262 tf->next = tmp_file_list;
264 memcpy (tf->name, "ldXXXXXX", sizeof (tf->name));
267 fd = mkstemp (*fname);
269 *fname = mktemp (*fname);
272 fd = open (fname, O_RDWR | O_CREAT | O_EXCL, 0600);
278 spu_elf_open_overlay_script (void)
282 if (auto_overlay_file == NULL)
284 int fd = new_tmp_file (&auto_overlay_file);
287 script = fdopen (fd, "w");
290 script = fopen (auto_overlay_file, "w");
295 einfo ("%F%P: can not open script: %E\n");
301 spu_elf_relink (void)
303 char **argv = xmalloc ((my_argc + 4) * sizeof (*argv));
305 memcpy (argv, my_argv, my_argc * sizeof (*argv));
306 argv[my_argc++] = "--no-auto-overlay";
307 if (tmp_file_list->name == auto_overlay_file)
308 argv[my_argc - 1] = concat (argv[my_argc - 1], "=",
309 auto_overlay_file, (const char *) NULL);
310 argv[my_argc++] = "-T";
311 argv[my_argc++] = auto_overlay_file;
313 execvp (argv[0], (char *const *) argv);
318 /* Final emulation specific call. */
321 gld${EMULATION_NAME}_finish (void)
325 need_laying_out = bfd_elf_discard_info (link_info.output_bfd, &link_info);
327 gld${EMULATION_NAME}_map_segments (need_laying_out);
329 if (is_spu_target ())
331 if (params.local_store_lo < params.local_store_hi)
335 s = spu_elf_check_vma (&link_info);
336 if (s != NULL && !params.auto_overlay)
337 einfo ("%X%P: %A exceeds local store range\n", s);
339 else if (params.auto_overlay)
340 einfo ("%P: --auto-overlay ignored with zero local store range\n");
342 if (!spu_elf_build_stubs (&link_info))
343 einfo ("%F%P: can not build overlay stubs: %E\n");
350 gld${EMULATION_NAME}_choose_target (int argc, char *argv[])
354 return ldemul_default_target (argc, argv);
359 if grep -q 'ld_elf.*ppc.*_emulation' ldemul-list.h; then
361 #include "filenames.h"
363 #include <sys/wait.h>
366 base_name (const char *path)
368 const char *file = strrchr (path, '/');
369 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
371 char *bslash = strrchr (path, '\\\\');
373 if (file == NULL || (bslash != NULL && bslash > file))
388 /* This function is called when building a ppc32 or ppc64 executable
389 to handle embedded spu images. */
390 extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *);
393 embedded_spu_file (lang_input_statement_type *entry, const char *flags)
402 union lang_statement_union **old_stat_tail;
403 union lang_statement_union **old_file_tail;
404 union lang_statement_union *new_ent;
405 lang_input_statement_type *search;
407 if (entry->the_bfd->format != bfd_object
408 || strcmp (entry->the_bfd->xvec->name, "elf32-spu") != 0
409 || (entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_EXEC
410 && entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_DYN))
413 /* Use the filename as the symbol marking the program handle struct. */
414 sym = base_name (entry->the_bfd->filename);
416 handle = xstrdup (sym);
417 for (p = handle; *p; ++p)
418 if (!(ISALNUM (*p) || *p == '$' || *p == '.'))
421 fd = new_tmp_file (&oname);
426 for (search = (lang_input_statement_type *) input_file_chain.head;
428 search = (lang_input_statement_type *) search->next_real_file)
429 if (search->filename != NULL)
431 const char *infile = base_name (search->filename);
433 if (strncmp (infile, "crtbegin", 8) == 0)
435 if (infile[8] == 'S')
436 flags = concat (flags, " -fPIC", (const char *) NULL);
437 else if (infile[8] == 'T')
438 flags = concat (flags, " -fpie", (const char *) NULL);
443 /* Use fork() and exec() rather than system() so that we don't
444 need to worry about quoting args. */
448 cmd[3] = entry->the_bfd->filename;
451 if (trace_file_tries)
453 info_msg (_("running: %s \"%s\" \"%s\" \"%s\" \"%s\"\n"),
454 cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]);
463 execvp (cmd[0], (char *const *) cmd);
464 if (strcmp ("embedspu", EMBEDSPU) != 0)
467 execvp (cmd[0], (char *const *) cmd);
473 #define WAITFOR(PID, STAT) waitpid (PID, STAT, 0)
475 #define WAITFOR(PID, STAT) wait (STAT)
477 if (WAITFOR (pid, &status) != pid
478 || !WIFEXITED (status)
479 || WEXITSTATUS (status) != 0)
483 old_stat_tail = stat_ptr->tail;
484 old_file_tail = input_file_chain.tail;
485 if (lang_add_input_file (oname, lang_input_file_is_file_enum, NULL) == NULL)
488 /* lang_add_input_file put the new list entry at the end of the statement
489 and input file lists. Move it to just after the current entry. */
490 new_ent = *old_stat_tail;
491 *old_stat_tail = NULL;
492 stat_ptr->tail = old_stat_tail;
493 *old_file_tail = NULL;
494 input_file_chain.tail = old_file_tail;
495 new_ent->header.next = entry->header.next;
496 entry->header.next = new_ent;
497 new_ent->input_statement.next_real_file = entry->next_real_file;
498 entry->next_real_file = new_ent;
500 /* Ensure bfd sections are excluded from the output. */
501 bfd_section_list_clear (entry->the_bfd);
502 entry->loaded = TRUE;
509 # Define some shell vars to insert bits of code into the standard elf
510 # parse_args and list_options functions.
512 PARSE_AND_LIST_PROLOGUE='
513 #define OPTION_SPU_PLUGIN 301
514 #define OPTION_SPU_NO_OVERLAYS (OPTION_SPU_PLUGIN + 1)
515 #define OPTION_SPU_STUB_SYMS (OPTION_SPU_NO_OVERLAYS + 1)
516 #define OPTION_SPU_NON_OVERLAY_STUBS (OPTION_SPU_STUB_SYMS + 1)
517 #define OPTION_SPU_LOCAL_STORE (OPTION_SPU_NON_OVERLAY_STUBS + 1)
518 #define OPTION_SPU_STACK_ANALYSIS (OPTION_SPU_LOCAL_STORE + 1)
519 #define OPTION_SPU_STACK_SYMS (OPTION_SPU_STACK_ANALYSIS + 1)
520 #define OPTION_SPU_AUTO_OVERLAY (OPTION_SPU_STACK_SYMS + 1)
521 #define OPTION_SPU_AUTO_RELINK (OPTION_SPU_AUTO_OVERLAY + 1)
522 #define OPTION_SPU_OVERLAY_RODATA (OPTION_SPU_AUTO_RELINK + 1)
523 #define OPTION_SPU_FIXED_SPACE (OPTION_SPU_OVERLAY_RODATA + 1)
524 #define OPTION_SPU_RESERVED_SPACE (OPTION_SPU_FIXED_SPACE + 1)
525 #define OPTION_SPU_EXTRA_STACK (OPTION_SPU_RESERVED_SPACE + 1)
526 #define OPTION_SPU_NO_AUTO_OVERLAY (OPTION_SPU_EXTRA_STACK + 1)
529 PARSE_AND_LIST_LONGOPTS='
530 { "plugin", no_argument, NULL, OPTION_SPU_PLUGIN },
531 { "no-overlays", no_argument, NULL, OPTION_SPU_NO_OVERLAYS },
532 { "emit-stub-syms", no_argument, NULL, OPTION_SPU_STUB_SYMS },
533 { "extra-overlay-stubs", no_argument, NULL, OPTION_SPU_NON_OVERLAY_STUBS },
534 { "local-store", required_argument, NULL, OPTION_SPU_LOCAL_STORE },
535 { "stack-analysis", no_argument, NULL, OPTION_SPU_STACK_ANALYSIS },
536 { "emit-stack-syms", no_argument, NULL, OPTION_SPU_STACK_SYMS },
537 { "auto-overlay", optional_argument, NULL, OPTION_SPU_AUTO_OVERLAY },
538 { "auto-relink", no_argument, NULL, OPTION_SPU_AUTO_RELINK },
539 { "overlay-rodata", no_argument, NULL, OPTION_SPU_OVERLAY_RODATA },
540 { "fixed-space", required_argument, NULL, OPTION_SPU_FIXED_SPACE },
541 { "reserved-space", required_argument, NULL, OPTION_SPU_RESERVED_SPACE },
542 { "extra-stack-space", required_argument, NULL, OPTION_SPU_EXTRA_STACK },
543 { "no-auto-overlay", optional_argument, NULL, OPTION_SPU_NO_AUTO_OVERLAY },
546 PARSE_AND_LIST_OPTIONS='
548 --plugin Make SPU plugin.\n\
549 --no-overlays No overlay handling.\n\
550 --emit-stub-syms Add symbols on overlay call stubs.\n\
551 --extra-overlay-stubs Add stubs on all calls out of overlay regions.\n\
552 --local-store=lo:hi Valid address range.\n\
553 --stack-analysis Estimate maximum stack requirement.\n\
554 --emit-stack-syms Add sym giving stack needed for each func.\n\
555 --auto-overlay [=filename] Create an overlay script in filename if\n\
556 executable does not fit in local store.\n\
557 --auto-relink Rerun linker using auto-overlay script.\n\
558 --overlay-rodata Place read-only data with associated function\n\
560 --fixed-space=bytes Local store for non-overlay code and data.\n\
561 --reserved-space=bytes Local store for stack and heap. If not specified\n\
562 ld will estimate stack size and assume no heap.\n\
563 --extra-stack-space=bytes Space for negative sp access (default 2000) if\n\
564 --reserved-space not given.\n"
568 PARSE_AND_LIST_ARGS_CASES='
569 case OPTION_SPU_PLUGIN:
573 case OPTION_SPU_NO_OVERLAYS:
574 params.ovly_flavour = ovly_none;
577 case OPTION_SPU_STUB_SYMS:
578 params.emit_stub_syms = 1;
581 case OPTION_SPU_NON_OVERLAY_STUBS:
582 params.non_overlay_stubs = 1;
585 case OPTION_SPU_LOCAL_STORE:
588 params.local_store_lo = strtoul (optarg, &end, 0);
589 if (*end == '\'':'\'')
591 params.local_store_hi = strtoul (end + 1, &end, 0);
595 einfo (_("%P%F: invalid --local-store address range `%s'\''\n"), optarg);
599 case OPTION_SPU_STACK_ANALYSIS:
600 params.stack_analysis = 1;
603 case OPTION_SPU_STACK_SYMS:
604 params.emit_stack_syms = 1;
607 case OPTION_SPU_AUTO_OVERLAY:
608 params.auto_overlay |= 1;
611 auto_overlay_file = optarg;
616 case OPTION_SPU_AUTO_RELINK:
617 params.auto_overlay |= 2;
620 case OPTION_SPU_OVERLAY_RODATA:
621 params.auto_overlay |= 4;
624 case OPTION_SPU_FIXED_SPACE:
627 params.auto_overlay_fixed = strtoul (optarg, &end, 0);
629 einfo (_("%P%F: invalid --fixed-space value `%s'\''\n"), optarg);
633 case OPTION_SPU_RESERVED_SPACE:
636 params.auto_overlay_reserved = strtoul (optarg, &end, 0);
638 einfo (_("%P%F: invalid --reserved-space value `%s'\''\n"), optarg);
642 case OPTION_SPU_EXTRA_STACK:
645 params.extra_stack_space = strtol (optarg, &end, 0);
647 einfo (_("%P%F: invalid --extra-stack-space value `%s'\''\n"), optarg);
651 case OPTION_SPU_NO_AUTO_OVERLAY:
652 params.auto_overlay = 0;
658 if (tmp_file_list == NULL)
661 len = strlen (optarg) + 1;
662 tf = xmalloc (sizeof (*tf) - sizeof (tf->name) + len);
663 memcpy (tf->name, optarg, len);
664 tf->next = tmp_file_list;
671 LDEMUL_AFTER_OPEN=spu_after_open
672 LDEMUL_BEFORE_ALLOCATION=spu_before_allocation
673 LDEMUL_FINISH=gld${EMULATION_NAME}_finish
674 LDEMUL_CHOOSE_TARGET=gld${EMULATION_NAME}_choose_target