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 /* Non-zero if no overlay processing should be done. */
30 static int no_overlays = 0;
32 /* Non-zero if we want stubs on all calls out of overlay regions. */
33 static int non_overlay_stubs = 0;
35 /* Whether to emit symbols for stubs. */
36 static int emit_stub_syms = 0;
38 /* Non-zero to perform stack space analysis. */
39 static int stack_analysis = 0;
41 /* Whether to emit symbols with stack requirements for each function. */
42 static int emit_stack_syms = 0;
44 /* Range of valid addresses for loadable sections. */
45 static bfd_vma local_store_lo = 0;
46 static bfd_vma local_store_hi = 0x3ffff;
48 /* Control --auto-overlay feature. */
49 static int auto_overlay = 0;
50 static char *auto_overlay_file = 0;
51 static unsigned int auto_overlay_fixed = 0;
52 static unsigned int auto_overlay_reserved = 0;
53 static int extra_stack_space = 2000;
57 static const char ovl_mgr[] = {
60 if ! cat ${srcdir}/emultempl/spu_ovl.o_c >> e${EMULATION_NAME}.c
62 echo >&2 "Missing ${srcdir}/emultempl/spu_ovl.o_c"
63 echo >&2 "You must build gas/as-new with --target=spu to build spu_ovl.o"
70 static const struct _ovl_stream ovl_mgr_stream = {
72 ovl_mgr + sizeof (ovl_mgr)
79 extern const bfd_target bfd_elf32_spu_vec;
81 return link_info.output_bfd->xvec == &bfd_elf32_spu_vec;
84 /* Create our note section. */
90 && !link_info.relocatable
91 && link_info.input_bfds != NULL
92 && !spu_elf_create_sections (&link_info,
93 stack_analysis, emit_stack_syms))
94 einfo ("%X%P: can not create note section: %E\n");
96 gld${EMULATION_NAME}_after_open ();
99 /* If O is NULL, add section S at the end of output section OUTPUT_NAME.
100 If O is not NULL, add section S at the beginning of output section O.
102 Really, we should be duplicating ldlang.c map_input_to_output_sections
103 logic here, ie. using the linker script to find where the section
104 goes. That's rather a lot of code, and we don't want to run
105 map_input_to_output_sections again because most sections are already
106 mapped. So cheat, and put the section in a fixed place, ignoring any
107 attempt via a linker script to put .stub, .ovtab, and built-in
108 overlay manager code somewhere else. */
111 spu_place_special_section (asection *s, asection *o, const char *output_name)
113 lang_output_section_statement_type *os;
115 os = lang_output_section_find (o != NULL ? o->name : output_name);
118 const char *save = s->name;
119 s->name = output_name;
120 gld${EMULATION_NAME}_place_orphan (s);
123 else if (o != NULL && os->children.head != NULL)
125 lang_statement_list_type add;
127 lang_list_init (&add);
128 lang_add_section (&add, s, os);
129 *add.tail = os->children.head;
130 os->children.head = add.head;
133 lang_add_section (&os->children, s, os);
135 s->output_section->size += s->size;
138 /* Load built-in overlay manager, and tweak overlay section alignment. */
141 spu_elf_load_ovl_mgr (void)
143 lang_output_section_statement_type *os;
144 struct elf_link_hash_entry *h;
146 h = elf_link_hash_lookup (elf_hash_table (&link_info),
147 "__ovly_load", FALSE, FALSE, FALSE);
150 && (h->root.type == bfd_link_hash_defined
151 || h->root.type == bfd_link_hash_defweak)
154 /* User supplied __ovly_load. */
156 else if (ovl_mgr_stream.start == ovl_mgr_stream.end)
157 einfo ("%F%P: no built-in overlay manager\n");
160 lang_input_statement_type *ovl_is;
162 ovl_is = lang_add_input_file ("builtin ovl_mgr",
163 lang_input_file_is_file_enum,
166 if (!spu_elf_open_builtin_lib (&ovl_is->the_bfd, &ovl_mgr_stream))
167 einfo ("%X%P: can not open built-in overlay manager: %E\n");
172 if (!load_symbols (ovl_is, NULL))
173 einfo ("%X%P: can not load built-in overlay manager: %E\n");
175 /* Map overlay manager sections to output sections. */
176 for (in = ovl_is->the_bfd->sections; in != NULL; in = in->next)
177 if ((in->flags & (SEC_ALLOC | SEC_LOAD))
178 == (SEC_ALLOC | SEC_LOAD))
179 spu_place_special_section (in, NULL, ".text");
183 /* Ensure alignment of overlay sections is sufficient. */
184 for (os = &lang_output_section_statement.head->output_section_statement;
187 if (os->bfd_section != NULL
188 && spu_elf_section_data (os->bfd_section) != NULL
189 && spu_elf_section_data (os->bfd_section)->u.o.ovl_index != 0)
191 if (os->bfd_section->alignment_power < 4)
192 os->bfd_section->alignment_power = 4;
194 /* Also ensure size rounds up. */
195 os->block_value = 16;
199 /* Go find if we need to do anything special for overlays. */
202 spu_before_allocation (void)
205 && !link_info.relocatable
208 /* Size the sections. This is premature, but we need to know the
209 rough layout so that overlays can be found. */
210 expld.phase = lang_mark_phase_enum;
211 expld.dataseg.phase = exp_dataseg_none;
212 one_lang_size_sections_pass (NULL, TRUE);
214 /* Find overlays by inspecting section vmas. */
215 if (spu_elf_find_overlays (&link_info))
219 if (auto_overlay != 0)
221 einfo ("%P: --auto-overlay ignored with user overlay script\n");
225 ret = spu_elf_size_stubs (&link_info,
226 spu_place_special_section,
229 einfo ("%X%P: can not size overlay stubs: %E\n");
231 spu_elf_load_ovl_mgr ();
234 /* We must not cache anything from the preliminary sizing. */
235 lang_reset_memory_regions ();
238 gld${EMULATION_NAME}_before_allocation ();
246 static struct tflist *tmp_file_list;
248 static void clean_tmp (void)
250 for (; tmp_file_list != NULL; tmp_file_list = tmp_file_list->next)
251 unlink (tmp_file_list->name);
255 new_tmp_file (char **fname)
260 if (tmp_file_list == NULL)
262 tf = xmalloc (sizeof (*tf));
263 tf->next = tmp_file_list;
265 memcpy (tf->name, "ldXXXXXX", sizeof (tf->name));
268 fd = mkstemp (*fname);
270 *fname = mktemp (*fname);
273 fd = open (fname, O_RDWR | O_CREAT | O_EXCL, 0600);
279 spu_elf_open_overlay_script (void)
283 if (auto_overlay_file == NULL)
285 int fd = new_tmp_file (&auto_overlay_file);
288 script = fdopen (fd, "w");
291 script = fopen (auto_overlay_file, "w");
296 einfo ("%F%P: can not open script: %E\n");
302 spu_elf_relink (void)
304 char **argv = xmalloc ((my_argc + 4) * sizeof (*argv));
306 memcpy (argv, my_argv, my_argc * sizeof (*argv));
307 argv[my_argc++] = "--no-auto-overlay";
308 if (tmp_file_list->name == auto_overlay_file)
309 argv[my_argc - 1] = concat (argv[my_argc - 1], "=",
310 auto_overlay_file, (const char *) NULL);
311 argv[my_argc++] = "-T";
312 argv[my_argc++] = auto_overlay_file;
314 execvp (argv[0], (char *const *) argv);
319 /* Final emulation specific call. */
322 gld${EMULATION_NAME}_finish (void)
326 need_laying_out = bfd_elf_discard_info (link_info.output_bfd, &link_info);
328 gld${EMULATION_NAME}_map_segments (need_laying_out);
330 if (is_spu_target ())
332 if (local_store_lo < local_store_hi)
336 s = spu_elf_check_vma (&link_info, auto_overlay,
337 local_store_lo, local_store_hi,
338 auto_overlay_fixed, auto_overlay_reserved,
340 spu_elf_load_ovl_mgr,
341 spu_elf_open_overlay_script,
343 if (s != NULL && !auto_overlay)
344 einfo ("%X%P: %A exceeds local store range\n", s);
346 else if (auto_overlay)
347 einfo ("%P: --auto-overlay ignored with zero local store range\n");
349 if (!spu_elf_build_stubs (&link_info,
350 emit_stub_syms || link_info.emitrelocations))
351 einfo ("%F%P: can not build overlay stubs: %E\n");
358 gld${EMULATION_NAME}_choose_target (int argc, char *argv[])
362 return ldemul_default_target (argc, argv);
367 if grep -q 'ld_elf.*ppc.*_emulation' ldemul-list.h; then
369 #include "filenames.h"
371 #include <sys/wait.h>
374 base_name (const char *path)
376 const char *file = strrchr (path, '/');
377 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
379 char *bslash = strrchr (path, '\\\\');
381 if (file == NULL || (bslash != NULL && bslash > file))
396 /* This function is called when building a ppc32 or ppc64 executable
397 to handle embedded spu images. */
398 extern bfd_boolean embedded_spu_file (lang_input_statement_type *, const char *);
401 embedded_spu_file (lang_input_statement_type *entry, const char *flags)
410 union lang_statement_union **old_stat_tail;
411 union lang_statement_union **old_file_tail;
412 union lang_statement_union *new_ent;
413 lang_input_statement_type *search;
415 if (entry->the_bfd->format != bfd_object
416 || strcmp (entry->the_bfd->xvec->name, "elf32-spu") != 0
417 || (entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_EXEC
418 && entry->the_bfd->tdata.elf_obj_data->elf_header->e_type != ET_DYN))
421 /* Use the filename as the symbol marking the program handle struct. */
422 sym = base_name (entry->the_bfd->filename);
424 handle = xstrdup (sym);
425 for (p = handle; *p; ++p)
426 if (!(ISALNUM (*p) || *p == '$' || *p == '.'))
429 fd = new_tmp_file (&oname);
434 for (search = (lang_input_statement_type *) input_file_chain.head;
436 search = (lang_input_statement_type *) search->next_real_file)
437 if (search->filename != NULL)
439 const char *infile = base_name (search->filename);
441 if (strncmp (infile, "crtbegin", 8) == 0)
443 if (infile[8] == 'S')
444 flags = concat (flags, " -fPIC", (const char *) NULL);
445 else if (infile[8] == 'T')
446 flags = concat (flags, " -fpie", (const char *) NULL);
451 /* Use fork() and exec() rather than system() so that we don't
452 need to worry about quoting args. */
456 cmd[3] = entry->the_bfd->filename;
459 if (trace_file_tries)
461 info_msg (_("running: %s \"%s\" \"%s\" \"%s\" \"%s\"\n"),
462 cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]);
471 execvp (cmd[0], (char *const *) cmd);
472 if (strcmp ("embedspu", EMBEDSPU) != 0)
475 execvp (cmd[0], (char *const *) cmd);
481 #define WAITFOR(PID, STAT) waitpid (PID, STAT, 0)
483 #define WAITFOR(PID, STAT) wait (STAT)
485 if (WAITFOR (pid, &status) != pid
486 || !WIFEXITED (status)
487 || WEXITSTATUS (status) != 0)
491 old_stat_tail = stat_ptr->tail;
492 old_file_tail = input_file_chain.tail;
493 if (lang_add_input_file (oname, lang_input_file_is_file_enum, NULL) == NULL)
496 /* lang_add_input_file put the new list entry at the end of the statement
497 and input file lists. Move it to just after the current entry. */
498 new_ent = *old_stat_tail;
499 *old_stat_tail = NULL;
500 stat_ptr->tail = old_stat_tail;
501 *old_file_tail = NULL;
502 input_file_chain.tail = old_file_tail;
503 new_ent->header.next = entry->header.next;
504 entry->header.next = new_ent;
505 new_ent->input_statement.next_real_file = entry->next_real_file;
506 entry->next_real_file = new_ent;
508 /* Ensure bfd sections are excluded from the output. */
509 bfd_section_list_clear (entry->the_bfd);
510 entry->loaded = TRUE;
517 # Define some shell vars to insert bits of code into the standard elf
518 # parse_args and list_options functions.
520 PARSE_AND_LIST_PROLOGUE='
521 #define OPTION_SPU_PLUGIN 301
522 #define OPTION_SPU_NO_OVERLAYS (OPTION_SPU_PLUGIN + 1)
523 #define OPTION_SPU_STUB_SYMS (OPTION_SPU_NO_OVERLAYS + 1)
524 #define OPTION_SPU_NON_OVERLAY_STUBS (OPTION_SPU_STUB_SYMS + 1)
525 #define OPTION_SPU_LOCAL_STORE (OPTION_SPU_NON_OVERLAY_STUBS + 1)
526 #define OPTION_SPU_STACK_ANALYSIS (OPTION_SPU_LOCAL_STORE + 1)
527 #define OPTION_SPU_STACK_SYMS (OPTION_SPU_STACK_ANALYSIS + 1)
528 #define OPTION_SPU_AUTO_OVERLAY (OPTION_SPU_STACK_SYMS + 1)
529 #define OPTION_SPU_AUTO_RELINK (OPTION_SPU_AUTO_OVERLAY + 1)
530 #define OPTION_SPU_OVERLAY_RODATA (OPTION_SPU_AUTO_RELINK + 1)
531 #define OPTION_SPU_FIXED_SPACE (OPTION_SPU_OVERLAY_RODATA + 1)
532 #define OPTION_SPU_RESERVED_SPACE (OPTION_SPU_FIXED_SPACE + 1)
533 #define OPTION_SPU_EXTRA_STACK (OPTION_SPU_RESERVED_SPACE + 1)
534 #define OPTION_SPU_NO_AUTO_OVERLAY (OPTION_SPU_EXTRA_STACK + 1)
537 PARSE_AND_LIST_LONGOPTS='
538 { "plugin", no_argument, NULL, OPTION_SPU_PLUGIN },
539 { "no-overlays", no_argument, NULL, OPTION_SPU_NO_OVERLAYS },
540 { "emit-stub-syms", no_argument, NULL, OPTION_SPU_STUB_SYMS },
541 { "extra-overlay-stubs", no_argument, NULL, OPTION_SPU_NON_OVERLAY_STUBS },
542 { "local-store", required_argument, NULL, OPTION_SPU_LOCAL_STORE },
543 { "stack-analysis", no_argument, NULL, OPTION_SPU_STACK_ANALYSIS },
544 { "emit-stack-syms", no_argument, NULL, OPTION_SPU_STACK_SYMS },
545 { "auto-overlay", optional_argument, NULL, OPTION_SPU_AUTO_OVERLAY },
546 { "auto-relink", no_argument, NULL, OPTION_SPU_AUTO_RELINK },
547 { "overlay-rodata", no_argument, NULL, OPTION_SPU_OVERLAY_RODATA },
548 { "fixed-space", required_argument, NULL, OPTION_SPU_FIXED_SPACE },
549 { "reserved-space", required_argument, NULL, OPTION_SPU_RESERVED_SPACE },
550 { "extra-stack-space", required_argument, NULL, OPTION_SPU_EXTRA_STACK },
551 { "no-auto-overlay", optional_argument, NULL, OPTION_SPU_NO_AUTO_OVERLAY },
554 PARSE_AND_LIST_OPTIONS='
556 --plugin Make SPU plugin.\n\
557 --no-overlays No overlay handling.\n\
558 --emit-stub-syms Add symbols on overlay call stubs.\n\
559 --extra-overlay-stubs Add stubs on all calls out of overlay regions.\n\
560 --local-store=lo:hi Valid address range.\n\
561 --stack-analysis Estimate maximum stack requirement.\n\
562 --emit-stack-syms Add sym giving stack needed for each func.\n\
563 --auto-overlay [=filename] Create an overlay script in filename if\n\
564 executable does not fit in local store.\n\
565 --auto-relink Rerun linker using auto-overlay script.\n\
566 --overlay-rodata Place read-only data with associated function\n\
568 --fixed-space=bytes Local store for non-overlay code and data.\n\
569 --reserved-space=bytes Local store for stack and heap. If not specified\n\
570 ld will estimate stack size and assume no heap.\n\
571 --extra-stack-space=bytes Space for negative sp access (default 2000) if\n\
572 --reserved-space not given.\n"
576 PARSE_AND_LIST_ARGS_CASES='
577 case OPTION_SPU_PLUGIN:
581 case OPTION_SPU_NO_OVERLAYS:
585 case OPTION_SPU_STUB_SYMS:
589 case OPTION_SPU_NON_OVERLAY_STUBS:
590 non_overlay_stubs = 1;
593 case OPTION_SPU_LOCAL_STORE:
596 local_store_lo = strtoul (optarg, &end, 0);
597 if (*end == '\'':'\'')
599 local_store_hi = strtoul (end + 1, &end, 0);
603 einfo (_("%P%F: invalid --local-store address range `%s'\''\n"), optarg);
607 case OPTION_SPU_STACK_ANALYSIS:
611 case OPTION_SPU_STACK_SYMS:
615 case OPTION_SPU_AUTO_OVERLAY:
619 auto_overlay_file = optarg;
624 case OPTION_SPU_AUTO_RELINK:
628 case OPTION_SPU_OVERLAY_RODATA:
632 case OPTION_SPU_FIXED_SPACE:
635 auto_overlay_fixed = strtoul (optarg, &end, 0);
637 einfo (_("%P%F: invalid --fixed-space value `%s'\''\n"), optarg);
641 case OPTION_SPU_RESERVED_SPACE:
644 auto_overlay_reserved = strtoul (optarg, &end, 0);
646 einfo (_("%P%F: invalid --reserved-space value `%s'\''\n"), optarg);
650 case OPTION_SPU_EXTRA_STACK:
653 extra_stack_space = strtol (optarg, &end, 0);
655 einfo (_("%P%F: invalid --extra-stack-space value `%s'\''\n"), optarg);
659 case OPTION_SPU_NO_AUTO_OVERLAY:
666 if (tmp_file_list == NULL)
669 len = strlen (optarg) + 1;
670 tf = xmalloc (sizeof (*tf) - sizeof (tf->name) + len);
671 memcpy (tf->name, optarg, len);
672 tf->next = tmp_file_list;
679 LDEMUL_AFTER_OPEN=spu_after_open
680 LDEMUL_BEFORE_ALLOCATION=spu_before_allocation
681 LDEMUL_FINISH=gld${EMULATION_NAME}_finish
682 LDEMUL_CHOOSE_TARGET=gld${EMULATION_NAME}_choose_target