1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2006, 2007, 2008, 2009 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, 0, 0, 0,
45 static unsigned int no_overlays = 0;
46 static unsigned int num_lines_set = 0;
47 static unsigned int line_size_set = 0;
48 static char *auto_overlay_file = 0;
52 static const char ovl_mgr[] = {
55 if ! cat ${srcdir}/emultempl/spu_ovl.o_c >> e${EMULATION_NAME}.c
57 echo >&2 "Missing ${srcdir}/emultempl/spu_ovl.o_c"
58 echo >&2 "You must build gas/as-new with --target=spu"
65 static const char icache_mgr[] = {
68 if ! cat ${srcdir}/emultempl/spu_icache.o_c >> e${EMULATION_NAME}.c
70 echo >&2 "Missing ${srcdir}/emultempl/spu_icache.o_c"
71 echo >&2 "You must build gas/as-new with --target=spu"
78 static const struct _ovl_stream ovl_mgr_stream = {
80 ovl_mgr + sizeof (ovl_mgr)
83 static const struct _ovl_stream icache_mgr_stream = {
85 icache_mgr + sizeof (icache_mgr)
92 extern const bfd_target bfd_elf32_spu_vec;
94 return link_info.output_bfd->xvec == &bfd_elf32_spu_vec;
97 /* Create our note section. */
100 spu_after_open (void)
102 if (is_spu_target ())
104 /* Pass params to backend. */
105 if ((params.auto_overlay & AUTO_OVERLAY) == 0)
106 params.auto_overlay = 0;
107 params.emit_stub_syms |= link_info.emitrelocations;
108 spu_elf_setup (&link_info, ¶ms);
110 if (link_info.relocatable)
111 lang_add_unique (".text.ia.*");
113 if (!link_info.relocatable
114 && link_info.input_bfds != NULL
115 && !spu_elf_create_sections (&link_info))
116 einfo ("%X%P: can not create note section: %E\n");
119 gld${EMULATION_NAME}_after_open ();
122 /* If O is NULL, add section S at the end of output section OUTPUT_NAME.
123 If O is not NULL, add section S at the beginning of output section O,
124 except for soft-icache which adds to the end.
126 Really, we should be duplicating ldlang.c map_input_to_output_sections
127 logic here, ie. using the linker script to find where the section
128 goes. That's rather a lot of code, and we don't want to run
129 map_input_to_output_sections again because most sections are already
130 mapped. So cheat, and put the section in a fixed place, ignoring any
131 attempt via a linker script to put .stub, .ovtab, and built-in
132 overlay manager code somewhere else. */
135 spu_place_special_section (asection *s, asection *o, const char *output_name)
137 lang_output_section_statement_type *os;
140 output_name = o->name;
141 os = lang_output_section_find (output_name);
144 os = gld${EMULATION_NAME}_place_orphan (s, output_name, 0);
145 os->addr_tree = NULL;
147 else if (params.ovly_flavour != ovly_soft_icache
148 && o != NULL && os->children.head != NULL)
150 lang_statement_list_type add;
152 lang_list_init (&add);
153 lang_add_section (&add, s, os);
154 *add.tail = os->children.head;
155 os->children.head = add.head;
159 if (params.ovly_flavour == ovly_soft_icache && o != NULL)
161 /* Pad this stub section so that it finishes at the
162 end of the icache line. */
165 push_stat_ptr (&os->children);
166 e_size = exp_intop (params.line_size - s->size);
167 lang_add_assignment (exp_assop ('=', ".", e_size));
170 lang_add_section (&os->children, s, os);
173 s->output_section->size += s->size;
176 /* Load built-in overlay manager. */
179 spu_elf_load_ovl_mgr (void)
181 struct elf_link_hash_entry *h;
182 const char *ovly_mgr_entry;
183 const struct _ovl_stream *mgr_stream;
184 bfd_size_type total = 0;
186 ovly_mgr_entry = "__ovly_load";
187 mgr_stream = &ovl_mgr_stream;
188 if (params.ovly_flavour == ovly_soft_icache)
190 ovly_mgr_entry = "__icache_br_handler";
191 mgr_stream = &icache_mgr_stream;
193 h = elf_link_hash_lookup (elf_hash_table (&link_info),
194 ovly_mgr_entry, FALSE, FALSE, FALSE);
197 && (h->root.type == bfd_link_hash_defined
198 || h->root.type == bfd_link_hash_defweak)
201 /* User supplied __ovly_load. */
203 else if (mgr_stream->start == mgr_stream->end)
204 einfo ("%F%P: no built-in overlay manager\n");
207 lang_input_statement_type *ovl_is;
209 ovl_is = lang_add_input_file ("builtin ovl_mgr",
210 lang_input_file_is_file_enum,
213 if (!spu_elf_open_builtin_lib (&ovl_is->the_bfd, mgr_stream))
214 einfo ("%X%P: can not open built-in overlay manager: %E\n");
219 if (!load_symbols (ovl_is, NULL))
220 einfo ("%X%P: can not load built-in overlay manager: %E\n");
222 /* Map overlay manager sections to output sections.
223 First try for a matching output section name, if that
224 fails then try mapping .abc.xyz to .abc, otherwise map
226 for (in = ovl_is->the_bfd->sections; in != NULL; in = in->next)
227 if ((in->flags & (SEC_ALLOC | SEC_LOAD))
228 == (SEC_ALLOC | SEC_LOAD))
230 const char *oname = in->name;
231 if (strncmp (in->name, ".ovl.init", 9) != 0)
234 if (!lang_output_section_find (oname))
236 lang_output_section_statement_type *os = NULL;
237 char *p = strchr (oname + 1, '.');
240 size_t len = p - oname;
241 p = memcpy (xmalloc (len + 1), oname, len);
243 os = lang_output_section_find (p);
253 spu_place_special_section (in, NULL, oname);
260 /* Go find if we need to do anything special for overlays. */
263 spu_before_allocation (void)
266 && !link_info.relocatable
271 /* Size the sections. This is premature, but we need to know the
272 rough layout so that overlays can be found. */
273 expld.phase = lang_mark_phase_enum;
274 expld.dataseg.phase = exp_dataseg_none;
275 one_lang_size_sections_pass (NULL, TRUE);
277 /* Find overlays by inspecting section vmas. */
278 ret = spu_elf_find_overlays (&link_info);
280 einfo ("%X%P: can not find overlays: %E\n");
283 lang_output_section_statement_type *os;
285 if (params.auto_overlay != 0)
287 einfo ("%P: --auto-overlay ignored with user overlay script\n");
288 params.auto_overlay = 0;
291 /* Ensure alignment of overlay sections is sufficient. */
292 for (os = &lang_output_section_statement.head->output_section_statement;
295 if (os->bfd_section != NULL
296 && spu_elf_section_data (os->bfd_section) != NULL
297 && spu_elf_section_data (os->bfd_section)->u.o.ovl_index != 0)
299 if (os->bfd_section->alignment_power < 4)
300 os->bfd_section->alignment_power = 4;
302 /* Also ensure size rounds up. */
303 os->block_value = 16;
306 ret = spu_elf_size_stubs (&link_info);
308 einfo ("%X%P: can not size overlay stubs: %E\n");
310 spu_elf_load_ovl_mgr ();
313 /* We must not cache anything from the preliminary sizing. */
314 lang_reset_memory_regions ();
317 gld${EMULATION_NAME}_before_allocation ();
325 static struct tflist *tmp_file_list;
327 static void clean_tmp (void)
329 for (; tmp_file_list != NULL; tmp_file_list = tmp_file_list->next)
330 unlink (tmp_file_list->name);
334 new_tmp_file (char **fname)
339 if (tmp_file_list == NULL)
341 tf = xmalloc (sizeof (*tf));
342 tf->next = tmp_file_list;
344 memcpy (tf->name, "ldXXXXXX", sizeof (tf->name));
347 fd = mkstemp (*fname);
349 *fname = mktemp (*fname);
352 fd = open (fname, O_RDWR | O_CREAT | O_EXCL, 0600);
358 spu_elf_open_overlay_script (void)
362 if (auto_overlay_file == NULL)
364 int fd = new_tmp_file (&auto_overlay_file);
367 script = fdopen (fd, "w");
370 script = fopen (auto_overlay_file, "w");
375 einfo ("%F%P: can not open script: %E\n");
381 spu_elf_relink (void)
383 char **argv = xmalloc ((my_argc + 4) * sizeof (*argv));
385 memcpy (argv, my_argv, my_argc * sizeof (*argv));
386 argv[my_argc++] = "--no-auto-overlay";
387 if (tmp_file_list->name == auto_overlay_file)
388 argv[my_argc - 1] = concat (argv[my_argc - 1], "=",
389 auto_overlay_file, (const char *) NULL);
390 argv[my_argc++] = "-T";
391 argv[my_argc++] = auto_overlay_file;
393 execvp (argv[0], (char *const *) argv);
398 /* Final emulation specific call. */
401 gld${EMULATION_NAME}_finish (void)
405 need_laying_out = bfd_elf_discard_info (link_info.output_bfd, &link_info);
407 gld${EMULATION_NAME}_map_segments (need_laying_out);
409 if (is_spu_target ())
411 if (params.local_store_lo < params.local_store_hi)
415 s = spu_elf_check_vma (&link_info);
416 if (s != NULL && !params.auto_overlay)
417 einfo ("%X%P: %A exceeds local store range\n", s);
419 else if (params.auto_overlay)
420 einfo ("%P: --auto-overlay ignored with zero local store range\n");
427 gld${EMULATION_NAME}_choose_target (int argc, char *argv[])
431 return ldemul_default_target (argc, argv);
436 if grep -q 'ld_elf.*ppc.*_emulation' ldemul-list.h; then
438 #include "filenames.h"
440 #include <sys/wait.h>
443 base_name (const char *path)
445 const char *file = strrchr (path, '/');
446 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
448 char *bslash = strrchr (path, '\\\\');
450 if (file == NULL || (bslash != NULL && bslash > file))
465 /* This function is called when building a ppc32 or ppc64 executable
466 to handle embedded spu images. */
467 extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *);
470 embedded_spu_file (lang_input_statement_type *entry, const char *flags)
479 union lang_statement_union **old_stat_tail;
480 union lang_statement_union **old_file_tail;
481 union lang_statement_union *new_ent;
482 lang_input_statement_type *search;
484 if (entry->the_bfd->format != bfd_object
485 || strcmp (entry->the_bfd->xvec->name, "elf32-spu") != 0
486 || (entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_EXEC
487 && entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_DYN))
490 /* Use the filename as the symbol marking the program handle struct. */
491 sym = base_name (entry->the_bfd->filename);
493 handle = xstrdup (sym);
494 for (p = handle; *p; ++p)
495 if (!(ISALNUM (*p) || *p == '$' || *p == '.'))
498 fd = new_tmp_file (&oname);
503 for (search = (lang_input_statement_type *) input_file_chain.head;
505 search = (lang_input_statement_type *) search->next_real_file)
506 if (search->filename != NULL)
508 const char *infile = base_name (search->filename);
510 if (strncmp (infile, "crtbegin", 8) == 0)
512 if (infile[8] == 'S')
513 flags = concat (flags, " -fPIC", (const char *) NULL);
514 else if (infile[8] == 'T')
515 flags = concat (flags, " -fpie", (const char *) NULL);
520 /* Use fork() and exec() rather than system() so that we don't
521 need to worry about quoting args. */
525 cmd[3] = entry->the_bfd->filename;
528 if (trace_file_tries)
530 info_msg (_("running: %s \"%s\" \"%s\" \"%s\" \"%s\"\n"),
531 cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]);
540 execvp (cmd[0], (char *const *) cmd);
541 if (strcmp ("embedspu", EMBEDSPU) != 0)
544 execvp (cmd[0], (char *const *) cmd);
550 #define WAITFOR(PID, STAT) waitpid (PID, STAT, 0)
552 #define WAITFOR(PID, STAT) wait (STAT)
554 if (WAITFOR (pid, &status) != pid
555 || !WIFEXITED (status)
556 || WEXITSTATUS (status) != 0)
560 old_stat_tail = stat_ptr->tail;
561 old_file_tail = input_file_chain.tail;
562 if (lang_add_input_file (oname, lang_input_file_is_file_enum, NULL) == NULL)
565 /* lang_add_input_file puts the new list entry at the end of the statement
566 and input file lists. Move it to just after the current entry. */
567 new_ent = *old_stat_tail;
568 *old_stat_tail = NULL;
569 stat_ptr->tail = old_stat_tail;
570 *old_file_tail = NULL;
571 input_file_chain.tail = old_file_tail;
572 new_ent->header.next = entry->header.next;
573 entry->header.next = new_ent;
574 new_ent->input_statement.next_real_file = entry->next_real_file;
575 entry->next_real_file = new_ent;
577 /* Ensure bfd sections are excluded from the output. */
578 bfd_section_list_clear (entry->the_bfd);
579 entry->loaded = TRUE;
586 # Define some shell vars to insert bits of code into the standard elf
587 # parse_args and list_options functions.
589 PARSE_AND_LIST_PROLOGUE='
590 #define OPTION_SPU_PLUGIN 301
591 #define OPTION_SPU_NO_OVERLAYS (OPTION_SPU_PLUGIN + 1)
592 #define OPTION_SPU_COMPACT_STUBS (OPTION_SPU_NO_OVERLAYS + 1)
593 #define OPTION_SPU_STUB_SYMS (OPTION_SPU_COMPACT_STUBS + 1)
594 #define OPTION_SPU_NON_OVERLAY_STUBS (OPTION_SPU_STUB_SYMS + 1)
595 #define OPTION_SPU_LOCAL_STORE (OPTION_SPU_NON_OVERLAY_STUBS + 1)
596 #define OPTION_SPU_STACK_ANALYSIS (OPTION_SPU_LOCAL_STORE + 1)
597 #define OPTION_SPU_STACK_SYMS (OPTION_SPU_STACK_ANALYSIS + 1)
598 #define OPTION_SPU_AUTO_OVERLAY (OPTION_SPU_STACK_SYMS + 1)
599 #define OPTION_SPU_AUTO_RELINK (OPTION_SPU_AUTO_OVERLAY + 1)
600 #define OPTION_SPU_OVERLAY_RODATA (OPTION_SPU_AUTO_RELINK + 1)
601 #define OPTION_SPU_SOFT_ICACHE (OPTION_SPU_OVERLAY_RODATA + 1)
602 #define OPTION_SPU_LINE_SIZE (OPTION_SPU_SOFT_ICACHE + 1)
603 #define OPTION_SPU_NUM_LINES (OPTION_SPU_LINE_SIZE + 1)
604 #define OPTION_SPU_LRLIVE (OPTION_SPU_NUM_LINES + 1)
605 #define OPTION_SPU_NON_IA_TEXT (OPTION_SPU_LRLIVE + 1)
606 #define OPTION_SPU_FIXED_SPACE (OPTION_SPU_NON_IA_TEXT + 1)
607 #define OPTION_SPU_RESERVED_SPACE (OPTION_SPU_FIXED_SPACE + 1)
608 #define OPTION_SPU_EXTRA_STACK (OPTION_SPU_RESERVED_SPACE + 1)
609 #define OPTION_SPU_NO_AUTO_OVERLAY (OPTION_SPU_EXTRA_STACK + 1)
612 PARSE_AND_LIST_LONGOPTS='
613 { "plugin", no_argument, NULL, OPTION_SPU_PLUGIN },
614 { "soft-icache", no_argument, NULL, OPTION_SPU_SOFT_ICACHE },
615 { "lrlive-analysis", no_argument, NULL, OPTION_SPU_LRLIVE },
616 { "num-lines", required_argument, NULL, OPTION_SPU_NUM_LINES },
617 { "line-size", required_argument, NULL, OPTION_SPU_LINE_SIZE },
618 { "non-ia-text", no_argument, NULL, OPTION_SPU_NON_IA_TEXT },
619 { "no-overlays", no_argument, NULL, OPTION_SPU_NO_OVERLAYS },
620 { "compact-stubs", no_argument, NULL, OPTION_SPU_COMPACT_STUBS },
621 { "emit-stub-syms", no_argument, NULL, OPTION_SPU_STUB_SYMS },
622 { "extra-overlay-stubs", no_argument, NULL, OPTION_SPU_NON_OVERLAY_STUBS },
623 { "local-store", required_argument, NULL, OPTION_SPU_LOCAL_STORE },
624 { "stack-analysis", no_argument, NULL, OPTION_SPU_STACK_ANALYSIS },
625 { "emit-stack-syms", no_argument, NULL, OPTION_SPU_STACK_SYMS },
626 { "auto-overlay", optional_argument, NULL, OPTION_SPU_AUTO_OVERLAY },
627 { "auto-relink", no_argument, NULL, OPTION_SPU_AUTO_RELINK },
628 { "overlay-rodata", no_argument, NULL, OPTION_SPU_OVERLAY_RODATA },
629 { "num-regions", required_argument, NULL, OPTION_SPU_NUM_LINES },
630 { "region-size", required_argument, NULL, OPTION_SPU_LINE_SIZE },
631 { "fixed-space", required_argument, NULL, OPTION_SPU_FIXED_SPACE },
632 { "reserved-space", required_argument, NULL, OPTION_SPU_RESERVED_SPACE },
633 { "extra-stack-space", required_argument, NULL, OPTION_SPU_EXTRA_STACK },
634 { "no-auto-overlay", optional_argument, NULL, OPTION_SPU_NO_AUTO_OVERLAY },
637 PARSE_AND_LIST_OPTIONS='
639 --plugin Make SPU plugin.\n\
640 --no-overlays No overlay handling.\n\
641 --compact-stubs Use smaller and possibly slower call stubs.\n\
642 --emit-stub-syms Add symbols on overlay call stubs.\n\
643 --extra-overlay-stubs Add stubs on all calls out of overlay regions.\n\
644 --local-store=lo:hi Valid address range.\n\
645 --stack-analysis Estimate maximum stack requirement.\n\
646 --emit-stack-syms Add sym giving stack needed for each func.\n\
647 --auto-overlay [=filename] Create an overlay script in filename if\n\
648 executable does not fit in local store.\n\
649 --auto-relink Rerun linker using auto-overlay script.\n\
650 --overlay-rodata Place read-only data with associated function\n\
652 --num-regions Number of overlay buffers (default 1).\n\
653 --region-size Size of overlay buffers (default 0, auto).\n\
654 --fixed-space=bytes Local store for non-overlay code and data.\n\
655 --reserved-space=bytes Local store for stack and heap. If not specified\n\
656 ld will estimate stack size and assume no heap.\n\
657 --extra-stack-space=bytes Space for negative sp access (default 2000) if\n\
658 --reserved-space not given.\n\
659 --soft-icache Generate software icache overlays.\n\
660 --num-lines Number of soft-icache lines (default 32).\n\
661 --line-size Size of soft-icache lines (default 1k).\n\
662 --non-ia-text Allow non-icache code in icache lines.\n\
663 --lrlive-analysis Scan function prologue for lr liveness.\n"
667 PARSE_AND_LIST_ARGS_CASES='
668 case OPTION_SPU_PLUGIN:
672 case OPTION_SPU_NO_OVERLAYS:
676 case OPTION_SPU_COMPACT_STUBS:
677 params.compact_stub = 1;
680 case OPTION_SPU_STUB_SYMS:
681 params.emit_stub_syms = 1;
684 case OPTION_SPU_NON_OVERLAY_STUBS:
685 params.non_overlay_stubs = 1;
688 case OPTION_SPU_LOCAL_STORE:
691 params.local_store_lo = strtoul (optarg, &end, 0);
692 if (*end == '\'':'\'')
694 params.local_store_hi = strtoul (end + 1, &end, 0);
698 einfo (_("%P%F: invalid --local-store address range `%s'\''\n"), optarg);
702 case OPTION_SPU_STACK_ANALYSIS:
703 params.stack_analysis = 1;
706 case OPTION_SPU_STACK_SYMS:
707 params.emit_stack_syms = 1;
710 case OPTION_SPU_AUTO_OVERLAY:
711 params.auto_overlay |= 1;
714 auto_overlay_file = optarg;
719 case OPTION_SPU_AUTO_RELINK:
720 params.auto_overlay |= 2;
723 case OPTION_SPU_OVERLAY_RODATA:
724 params.auto_overlay |= 4;
727 case OPTION_SPU_SOFT_ICACHE:
728 params.ovly_flavour = ovly_soft_icache;
730 params.num_lines = 32;
731 else if ((params.num_lines & -params.num_lines) != params.num_lines)
732 einfo (_("%P%F: invalid --num-lines/--num-regions `%u'\''\n"),
735 params.line_size = 1024;
736 else if ((params.line_size & -params.line_size) != params.line_size)
737 einfo (_("%P%F: invalid --line-size/--region-size `%u'\''\n"),
741 case OPTION_SPU_LRLIVE:
742 params.lrlive_analysis = 1;
745 case OPTION_SPU_NON_IA_TEXT:
746 params.non_ia_text = 1;
749 case OPTION_SPU_NUM_LINES:
752 params.num_lines = strtoul (optarg, &end, 0);
755 && (params.ovly_flavour != ovly_soft_icache
756 || (params.num_lines & -params.num_lines) == params.num_lines))
758 einfo (_("%P%F: invalid --num-lines/--num-regions `%s'\''\n"), optarg);
762 case OPTION_SPU_LINE_SIZE:
765 params.line_size = strtoul (optarg, &end, 0);
768 && (params.ovly_flavour != ovly_soft_icache
769 || (params.line_size & -params.line_size) == params.line_size))
771 einfo (_("%P%F: invalid --line-size/--region-size `%s'\''\n"), optarg);
775 case OPTION_SPU_FIXED_SPACE:
778 params.auto_overlay_fixed = strtoul (optarg, &end, 0);
780 einfo (_("%P%F: invalid --fixed-space value `%s'\''\n"), optarg);
784 case OPTION_SPU_RESERVED_SPACE:
787 params.auto_overlay_reserved = strtoul (optarg, &end, 0);
789 einfo (_("%P%F: invalid --reserved-space value `%s'\''\n"), optarg);
793 case OPTION_SPU_EXTRA_STACK:
796 params.extra_stack_space = strtol (optarg, &end, 0);
798 einfo (_("%P%F: invalid --extra-stack-space value `%s'\''\n"), optarg);
802 case OPTION_SPU_NO_AUTO_OVERLAY:
803 params.auto_overlay = 0;
809 if (tmp_file_list == NULL)
812 len = strlen (optarg) + 1;
813 tf = xmalloc (sizeof (*tf) - sizeof (tf->name) + len);
814 memcpy (tf->name, optarg, len);
815 tf->next = tmp_file_list;
822 LDEMUL_AFTER_OPEN=spu_after_open
823 LDEMUL_BEFORE_ALLOCATION=spu_before_allocation
824 LDEMUL_FINISH=gld${EMULATION_NAME}_finish
825 LDEMUL_CHOOSE_TARGET=gld${EMULATION_NAME}_choose_target